Note that there are some explanatory texts on larger screens.

plurals
  1. POLaunching multiple tasks from a WCF service
    text
    copied!<p>I need to optimize a WCF service... it's quite a complex thing. My problem this time has to do with tasks (Task Parallel Library, .NET 4.0). What happens is that I launch several tasks when the service is invoked (using <code>Task.Factory.StartNew</code>) and then wait for them to finish:</p> <pre><code>Task.WaitAll(task1, task2, task3, task4, task5, task6); </code></pre> <p>Ok... what I see, and don't like, is that on the first call (sometimes the first 2-3 calls, if made quickly one after another), the final task starts much later than the others (I am looking at a case where it started 0.5 seconds after the others). I tried calling</p> <pre><code>ThreadPool.SetMinThreads(12*Environment.ProcessorCount, 20); </code></pre> <p>at the beginning of my service, but it doesn't seem to help.</p> <p>The tasks are all database-related: I'm reading from multiple databases and it has to take as little time as possible.</p> <p>Any idea why the last task is taking so long? Is there something I can do about it?</p> <p>Alternatively, should I use the thread pool directly? As it happens, in one case I'm looking at, one task had already ended before the last one started - I would had saved 0.2 seconds if I had reused that thread instead of waiting for a new one to be created. However, I can not be sure that <em>that</em> task will always end so quickly, so I can't put both requests in the same task.</p> <p>[Edit] The OS is Windows Server 2003, so there should be no connection limit. Also, it is hosted in IIS - I don't know if I should create regular threads or using the thread pool - which is the preferred version?</p> <p>[Edit] I've also tried using <code>Task.Factory.StartNew(action, TaskCreationOptions.LongRunning);</code> - it doesn't help, the last task still starts much later (around half a second later) than the rest.</p> <p>[Edit] MSDN<a href="http://msdn.microsoft.com/en-us/library/0ka9477y(v=vs.85).aspx" rel="noreferrer">1</a> says:</p> <blockquote> <p>The thread pool has a built-in delay (half a second in the .NET Framework version 2.0) before starting new idle threads. If your application periodically starts many tasks in a short time, a small increase in the number of idle threads can produce a significant increase in throughput. Setting the number of idle threads too high consumes system resources needlessly.</p> </blockquote> <p>However, as I said, I'm already calling SetMinThreads and it doesn't help.</p>
 

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