Home > ASP.NET > ASP.NET – Call Synchronous Method Asynchronously using C# ,.net (Fire and forget pattern)

ASP.NET – Call Synchronous Method Asynchronously using C# ,.net (Fire and forget pattern)


Hi Friends,

Many times we come across a situation when we need to call a method asynchronously and don’t want to wait till it completes its execution.


Call Method Asynchronously
  1. class Program
  2.     {
  3.         public delegate void AsyncMethodCaller(int Application_Type_Id);
  5.         public static void AsynchronousMethod(int Application_Type_Id)
  6.         {
  7.             Console.WriteLine("My Application_Id is {0}",Application_Type_Id);
  9.         }
  10.         static void Main()
  11.         {        
  12.            AsyncMethodCaller caller = new AsyncMethodCaller(AsynchronousMethod);
  13.            IAsyncResult result = caller.BeginInvoke(1, null, null);
  14.            Console.Read();
  16.         }
  18.     }


For Example In the above code, if I want to call AsynchronousMethod() and not wait for it to finish executing? In fact, to make things interesting, what if I didn’t care when it is finished?

Let’s say, I just wanted to call AsynchronousMethod() without waiting for any of the function calls to complete. Basically, doing something called Fire and Forget. You call the function, you don’t wait for it, and you just forget about it.

Note :

* BeginInvoke() is the line of code that executes the AsynchronousMethod() function. However, the control is returned to the caller right away, without waiting for AsynchronousMethod() to complete.

* The code above does not know when a call to AsynchronousMethod() completes.

* BeginInvoke() is used instead of Invoke(). For now, don’t worry about the parameters this function takes; I will cover that in more detail later.


How .NET works in background to make this Possible?

Once you ask the framework to call something asynchronously, it needs a thread to do the work. It can not be the current thread, because that would make the invocation synchronous (blocking). Instead, the runtime queues a request to execute the function on a thread from the .NET Thread Pool. You don’t really need to code anything for it, all of it happens in the background. But, just because it is all transparent doesn’t mean you should care about it. There are a few things to remember:

* AsynchronousMethod() is executed on a separate thread, a thread that belongs to the .NET Thread Pool.

* A .NET Thread Pool contains threads in it , and each time AsynchronousMethod() is called, it is going to be executed on one of these threads. You can’t control which one.

* The Thread Pool has its limits! Once all the threads are used, an async method invocation is queued until one of the threads from the pool is freed. This is called Thread Pool Starvation, and normally when it comes to that, performance is compromised.



Hope this Helps!!!!!!Smile

Categories: ASP.NET
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: