You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$10
For Premium members:
In the previous article we discussed the use of delegates to call methods asynchronously then we talked about Synchronization of threads in a Multithreading environment. In the previous code example we used the BeginInvoke() method to make the Async call.
Previous article I'm using the same example as in my previous article, so the following are all the terms and code from the previous example: //Simple delegate declaration public delegate int BinaryOp(int x, int y); //An Add() method that do some simple arithamtic operation public int Add(int a, int b) { Console.WriteLine("Add() running on thread {0}", Thread.CurrentThread.ManagedThreadId); Thread.Sleep(500); return (a + b); } BinaryOp bp = new BinaryOp(Add); IAsyncResult iftAr = bp.BeginInvoke(5, 5, null, null);
Just as a reminder, bp is the instance of a BinaryOP delegate that points to a simple Add() method that performs some basic arithmetic operations. We passed the arguments to BeginInvoke() with some null values. Now instead of using null in the parameters you'll use objects which are part of the signature of the BeginInvoke() method of dynamic class of delegate BinaryOp.BeginInvoke(int x, int y, AsyncCallback cb, Object state);Let's discuss what the last two parameters are for.
Note: In the previous example we used a Boolean variable in two different threads which is not thread safe. However to use such a shared variable the very good rule of thumb is to ensure that data that can be shared among multiple threads is locked down.Now when the secondary thread completes the Add() operation, the question should be where am I gonna call the EndInvoke()? Well you can call the EndInvoke in two places.
Inside the main just after the condition where you're waiting for the secondary thread to complete. But this does not satisfy the purpose of passing AsyncCallback.public delegate int BinaryOp(int x, int y); static void Main(string[] args) { Console.WriteLine("Main() running on thread {0}", Thread.CurrentThread.ManagedThreadId); Program p=new Program(); BinaryOp bp = new BinaryOp(p.Add); IAsyncResult iftAr = bp.BeginInvoke(5, 5, new AsyncCallback(p.AddComplete), null); while (!iftAr.AsyncWaitHandle.WaitOne(100,true)) { Console.WriteLine("Doing some work in Main()!"); } int result = bp.EndInvoke(iftAr); Console.WriteLine("5 + 5 ={0}", result); Console.Read(); }//An Add() method that do some simple arithamtic operation public int Add(int a, int b) { Console.WriteLine("Add() running on thread {0}", Thread.CurrentThread.ManagedThreadId); Thread.Sleep(500); return (a + b); } //Target of AsyncCallback delegate should match the following pattern public void AddComplete(IAsyncResult iftAr) { Console.WriteLine("AddComplete() running on thread {0}", Thread.CurrentThread.ManagedThreadId); Console.WriteLine("Operation completed."); }Output:Here you can see the AddComplete() is called after the Main() completed its execution. If you are looking deep into the code and find iftAr.AsyncWaitHandle.WaitOne(100,true)) as something alien statement then you should look at the Delegate and Async Programming Part I - C#.
The second and more interesting approach is showing the result by calling EndInovke() inside the AddComplete(). Now you should have a question here. The instance of the BinaryOp delegate is running on the primary thread in the Main(). How can we access it to secondary thread to call the EndInvoke()? The answer is the System.Runtime.Remoting.Messaging.AsyncResult class.
Programming List with C#