Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using delegates in ASP.NET to handle asynchronous operations

When calling BeginInvoke on a delegate, the action is executed on a separate thread. If called in ASP.NET does it use a CLR worker thread? Or does it use an IIS worker thread?

If the latter, then I will need to employ an asynchronous ASP.NET pattern to ensure the action is executed on a CLR worker thread. But I would rather not do that if the action ends up there upon BeginInvoke.

like image 909
John Livermore Avatar asked Nov 16 '11 14:11

John Livermore


People also ask

How can we make asynchronous method calls using delegates?

You can call methods asynchronously in four different ways using the BeginInvoke() and EndInvoke() methods of the Delegate class. The four different ways are using the EndInvoke pattern, WaitHandle, Polling pattern and using a Callback Pattern. In this article, we will see the Delegate EndInvoke Pattern.

Can we use Delegates for asynchronous method calls in C#?

Delegates enable you to call a synchronous method in an asynchronous manner. When you call a delegate synchronously, the Invoke method calls the target method directly on the current thread. If the BeginInvoke method is called, the common language runtime (CLR) queues the request and returns immediately to the caller.

How do I create async delegate?

To do this, you need to supply an instance of the AsyncCallback delegate as a parameter to BeginInvok(). This delegate will call a specified method automaticaly when the asynchronous call has completed. The method that AsyncCallback will invoke must taking IAsyncResult as a sole parameter and return nothing.

How do you call a synchronous method asynchronously in C#?

The simplest way to execute a method asynchronously is to start executing the method by calling the delegate's BeginInvoke method, do some work on the main thread, and then call the delegate's EndInvoke method. EndInvoke might block the calling thread because it does not return until the asynchronous call completes.


2 Answers

it uses a CLR worker thread.

as described in here

To begin with, ASP.NET uses the process-wide CLR thread pool to service requests (for more background on the CLR thread pool, see the .NET column in this issue).

EDIT:

another resource is this blog

Unfortunately, the thread used by BeginInvoke is actually taken from the same worker thread pool that is used by ASP.Net to handle Page Requests

like image 103
Mithir Avatar answered Sep 19 '22 15:09

Mithir


Thread usage/management is a bit different in IIS6, IIS7 and IIS 7.5.

Pretty detail and updated explanation here:

ASP.NET Thread Usage on IIS 7.5, IIS 7.0, and IIS 6.0

Not sure if this answers your question but a good read anyways.

like image 32
Aseem Gautam Avatar answered Sep 22 '22 15:09

Aseem Gautam