Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire and Forget (Asynch) ASP.NET Method Call

We have a service to update customer information to server. One service call takes around few seconds, which is normal.

Now we have a new page where at one instance around 35-50 Costumers information can be updated. Changing service interface to accept all customers together is out of question at this point.

I need to call a method (say "ProcessCustomerInfo"), which will loop through customers information and call web service 35-50 times. Calling service asynchronously is not of much use.

I need to call the method "ProcessCustomerInfo" asynchronously. I am trying to use RegisterAsyncTask for this. There are various examples available on web, but the problem is after initiating this call if I move away from this page, the processing stops.

Is it possible to implement Fire and Forget method call so that user can move away (Redirect to another page) from the page without stopping method processing?

like image 954
BinaryHacker Avatar asked Oct 12 '09 19:10

BinaryHacker


People also ask

What happens if async method is called without await?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Is asynchronous fire and forget?

Fire-and-Forget is most effective with asynchronous communication channels, which do not require the Originator to wait until the message is delivered to the Recipient. Instead, the Originator can pursue other tasks as soon as the messaging system has accepted the message.

How do I end async method?

You can cancel an asynchronous operation after a period of time by using the CancellationTokenSource. CancelAfter method if you don't want to wait for the operation to finish.

What is async call in C#?

An async method runs synchronously until it reaches its first await expression, at which point the method is suspended until the awaited task is complete. In the meantime, control returns to the caller of the method, as the example in the next section shows.


1 Answers

Details on: http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

Basically you can create a delegate which points to the method you want to run asynchronously and then kick it off with BeginInvoke.

// Declare the delegate - name it whatever you would like
public delegate void ProcessCustomerInfoDelegate();

// Instantiate the delegate and kick it off with BeginInvoke
ProcessCustomerInfoDelegate d = new ProcessCustomerInfoDelegate(ProcessCustomerInfo); 
simpleDelegate.BeginInvoke(null, null);

// The method which will run Asynchronously
void ProcessCustomerInfo()
{
   // this is where you can call your webservice 50 times
}
like image 53
Joel Beckham Avatar answered Oct 05 '22 01:10

Joel Beckham