At the moment of time, 300 tasks are started (this launch occurs periodically in a couple of minutes). A method that runs inside a task generates several asynchronous requests to an external source. Given that a lot of tasks, and the answer from an external source can be for several minutes, then because of this, unnecessary streams are created. Unnecessary in the sense that at the same time by the working machine they cannot be processed and it is necessary to spend time switching between them. The question is how can I limit the number of threads (i.e., to run 15 tasks in one thread as the workstation allows you to execute 20 threads in parallel) that will be used to process these tasks. Possible options are limiting the number of threads in ThreadPool or launching via Parallel.For with the MaxDegreeOfParallelism parameter set. But there is a condition that the completed asynchronous operation must continue to work in the stream in which it started / was launched. I will be glad to any help or sources where I can see the idea of ​​implementation.

Upd. Regarding why a thread is created to start an asynchronous operation. There are a lot of sources to which requests occur - and they are different. Therefore, one instance that is launched via Task.Run () is a call to only one external source. Those. 300 tasks are 300 different sources. Also, this data is further processed with its own logic.

  • one
    So you are creating a thread to start an asynchronous operation? What for? Run all asynchronous operations in your thread asynchronously. - tym32167
  • Clarified, thank you - sirishotka
  • one
    It is still not clear why you do not want to start all tasks from your stream, why are you creating new threads? Can I have code example? - tym32167
  • Plus to "run all three hundred in one thread, then collect the result". Stackoverflow.com/a/438952/213987 - AK ♦
  • If you are creating streams, then your calls to external sources are cpu bound, not io bound, i.e. actually synchronous, if you want to create a bunch of tasks, and not produce threads, make them so that calls to external sources are really asynchronous. - Primus Singularis

0