2

I noticed that code after return from ExecuteRegisteredAsyncTasks call is executed in same thread as code before this call. So I understand that manually calling ExecuteRegisteredAsyncTasks doesn't free the request-processing thread to service other requests and this method should be only automatically called to get scaling benefit?

On msdn is example code but help doesn't answer this question.

UPDATE

In my case, in PageAsyncTasks I am asynchronously calling web services (using Begin* and End*) so I am not using thread pool.

Pol
  • 5,064
  • 4
  • 32
  • 51

1 Answers1

5

In the MSDN example of the RegisterAsyncTask method, they simply declare an AsyncTaskDelegate which is invoked with Begin and End methods. When you asynchronously invoke a delegate it uses a thread from the thread pool so you are basically not getting any improvement in this case. The only speed improvement might be if you were executing multiple operations in parallel that would otherwise would be executed synchronously.

If you want to get a real performance boost you need to use IOCP (I/O Completion Ports). They are adapted to I/O intensive tasks such as network calls (database calls, web service calls, ...). Not suitable for CPU intensive tasks. So let's suppose that you want to perform an HTTP call using WebClient.DownloadStringAsync method. This method doesn't use a thread from the thread pool. It registers an IOCP with the kernel and fires the HTTP request. When it returns control to the caller no thread is being consumed and this during the whole HTTP request duration. Once the request finishes the IOCP is signaled and a thread is drawn from the thread pool to process the results.

Take a look at the asynchronous pages in ASP.NET article. IOCP are explained better there.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks. See my update. I just read this article but it doesn't answer this question. – Pol Feb 11 '12 at 10:18
  • @Pol, so if you are calling web services with the asynchronous versions of the methods you are not using threads from the thread pool. So to answer your question: yes, in this case the request processing threads is freed during the web service calls. – Darin Dimitrov Feb 11 '12 at 10:22
  • No matter if I call `ExecuteRegisteredAsyncTasks` directly in my code or if this is automatically called at the point in the page processing? In both cases request processing threads is freed during the web service calls? – Pol Feb 11 '12 at 15:02
  • @Pol, as long as you use IOCP for the long running operation you are not consuming worker threads. – Darin Dimitrov Feb 11 '12 at 15:06