Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take a look at <a href="http://www.albahari.com/threading/" rel="nofollow">this extensive article on threading</a>. Under section <a href="http://www.albahari.com/threading/part5.aspx#_Concurrent_Collections" rel="nofollow">Concurrent collections</a> you can find an example of a <code>producer/consumer</code> queue that executes tasks.</p> <p>As a general consideration you should not create 10k <code>Task</code> objects and <code>Run()</code> them all. Instead, create <em>n</em> <code>Task</code> instances that will execute the 10k methods you want to execute.</p> <p><strong>EDIT</strong></p> <p>If you want to use <code>ThreadPool</code> to execute the methods you want you'll have to take in consideration the following:</p> <ul> <li>what if one of your <code>Action</code>s throws an exception? <code>ThreadPool</code> does not marshal the exception to the calling thread so there is no way for you to catch it on the calling thread and it will bring your application down.</li> <li>how will you know that <em>all</em> the <code>Action</code>s are completed? <code>ThreadPool</code> does not offer a way to do this - it performs actions in a way that is very close to <em>fire and forget</em> strategy.</li> </ul> <p>On the other hand <code>Task Parallel Library</code>, which is built on top of <code>ThreadPool</code> offers elegant ways to deal with the problems above. The <code>PCQueue</code> example from the link above (<a href="http://www.albahari.com/threading/part5.aspx#_Concurrent_Collections" rel="nofollow">Concurrent collections</a>) shows you how to deal with exceptions thrown from the <code>Action</code>s you are invoking and provides a way to continue your application flow when <em>all</em> the actions are completed.</p> <p>You can use <code>PCQueue</code> like this:</p> <pre><code>var queue = new PCQueue(Environment.ProcessorCount); var tasks = tasksToRunInCurrentBatch.Select(t=&gt;queue.Enqueue(t.Value)).ToArray(); Task.WaitAll(tasks); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload