Note that there are some explanatory texts on larger screens.

plurals
  1. POtask background worker c#
    primarykey
    data
    text
    <p>Is there any change that a multiple Background Workers perform better than Tasks on 5 second running processes? I remember reading in a book that a Task is designed for short running processes.</p> <p>The reasong I ask is this:</p> <p>I have a process that takes 5 seconds to complete, and there are 4000 processes to complete. At first I did:</p> <pre><code>for (int i=0; i&lt;4000; i++) { Task.Factory.StartNewTask(action); } </code></pre> <p>and this had a poor performance (after the first minute, 3-4 tasks where completed, and the console application had 35 threads). Maybe this was stupid, but I thought that the thread pool will handle this kind of situation (it will put all actions in a queue, and when a thread is free, it will take an action and execute it).</p> <p>The second step now was to do manually Environment.ProcessorCount background workers, and all the actions to be placed in a ConcurentQueue. So the code would look something like this:</p> <pre><code>var workers = new List&lt;BackgroundWorker&gt;(); //initialize workers workers.ForEach((bk) =&gt; { bk.DoWork += (s, e) =&gt; { while (toDoActions.Count &gt; 0) { Action a; if (toDoActions.TryDequeue(out a)) { a(); } } } bk.RunWorkerAsync(); }); </code></pre> <p>This performed way better. It performed much better then the tasks even when I had 30 background workers (as much tasks as in the first case).</p> <p>LE:</p> <p>I start the Tasks like this:</p> <pre><code> public static Task IndexFile(string file) { Action&lt;object&gt; indexAction = new Action&lt;object&gt;((f) =&gt; { Index((string)f); }); return Task.Factory.StartNew(indexAction, file); } </code></pre> <p>And the Index method is this one:</p> <pre><code> private static void Index(string file) { AudioDetectionServiceReference.AudioDetectionServiceClient client = new AudioDetectionServiceReference.AudioDetectionServiceClient(); client.IndexCompleted += (s, e) =&gt; { if (e.Error != null) { if (FileError != null) { FileError(client, new FileIndexErrorEventArgs((string)e.UserState, e.Error)); } } else { if (FileIndexed != null) { FileIndexed(client, new FileIndexedEventArgs((string)e.UserState)); } } }; using (IAudio proxy = new BassProxy()) { List&lt;int&gt; max = new List&lt;int&gt;(); if (proxy.ReadFFTData(file, out max)) { while (max.Count &gt; 0 &amp;&amp; max.First() == 0) { max.RemoveAt(0); } while (max.Count &gt; 0 &amp;&amp; max.Last() == 0) { max.RemoveAt(max.Count - 1); } client.IndexAsync(max.ToArray(), file, file); } else { throw new CouldNotIndexException(file, "The audio proxy did not return any data for this file."); } } } </code></pre> <p>This methods reads from an mp3 file some data, using the Bass.net library. Then that data is sent to a WCF service, using the async method. The IndexFile(string file) method, which creates tasks is called for 4000 times in a for loop. Those two events, FileIndexed and FileError are not handled, so they are never thrown.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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