0

I have been tasked to take over an old bit of code that uses delegates.

SearchDelegate[] dlgt = new SearchDelegate[numSearches];
IAsyncResult[] ar = new IAsyncResult[numSearches];

It then does a loop to start multiple delegate functions

for (int i = 0; i < numSearches; i++)
{
    ar[i] = dlgt[i].BeginInvoke(....);
}

It then does a timed loop to get the results from the ar object.

It all seems to work fine. The issue I am having is that sometimes some of these delegate functions can take 3 to 4 seconds to start, even longer if the count goes above 10. Is this a common problem, or is there a setting I can tweak?

This is running on IIS. I can replicate the issue locally with the minimal machine resources being used.

Thanks all.

Daz

bommelding
  • 2,969
  • 9
  • 14
DazzlaJ
  • 57
  • 4

2 Answers2

1

can take 3 to 4 seconds to start

is caused by the threadpool. When all threads are busy it only slowly (2/second) creates new threads.

You could up the min amount of threads in the pool but especially for a web app you should research, test and measure that extensively. ASP.NET also is a big stakeholder in the threadpool.

bommelding
  • 2,969
  • 9
  • 14
0

BeginInvoke method dispatches actual work to a thread pool, as it's written in this article. It may take some time actually, when there are no available idle threads. Thread pool may decide to wait for some work items completion or to add additional threads, accounting min and max limits.

Some additional info may be found here The managed threadpool and there Simple description of worker and IO threads in net and at remarks section of this article as well ThreadPool.SetMinThreads.

You should be aware that the same thread pool is used for HTTP requests processing, therefore it's usually senseless to offload custom non IO-bound work to the thread pool in web apps, as it won't give you any benefits and may even hurt performance due to additional thread switches. While BeginInvoke doesn't look as an invocation of asynchronous IO operation.

It doesn't actually matter which concrete thread executes the work — client still have to wait for the response the same amount of time. Looks, like you may, probably, win some time by performing work in parallel, but it won't be possible under load as far as there won't be available threads at thread pool for processing both HTTP requests and your custom work items.

You may want to check this thread for some additional details on this theme. It's related to Task, but this doesn't matter as far as both BeginInvoke and Task.Run are using the same thread pool under the hood.

Uladzislaŭ
  • 1,680
  • 10
  • 13
  • Asp.Net thread pool is different from managed thread pool and is much more precious resource to be handled during programming – Mrinal Kamboj May 23 '18 at 10:01
  • @MrinalKamboj I do not think that such specific thing as ASP.NET thread pool even exists. IIS dispatches request processing to the same CLR Managed Thread Pool. Sure, defaults for pool under IIS may be different and there are additional config tweaks for max concurrent requests, etc, but these are insignificant for the OP's question. – Uladzislaŭ May 23 '18 at 16:11