Hmm. Lets get stared with the async call implementation in c#. The concept can be better understood using window based application (GUI application). Let us first create a new window project in c#. Follow me,
Note: I have attached the project with this, if you are not comfortable with that, then create a new one by following these steps.
- Click File > New > Project and select WindowApplication. Give it some name (say AsyncCalls)
- Now our idea is to have another class with some method that takes a long time for execution. For that, add a new class (Right click on the Project from solution explorer > Add > Class. Give it some name, like RemoteObject.cs)
- Implement a public method in this class. Lets name it GetTheMessage() with no params, no return type (for simplicity) and just add a MessageBox.Show(“foobarbaz”) statement with some string to show. As we have mentioned earlier, this method should take some time for execution. So lets add a Thread.Sleep(10000); statement before the MessageBox.Show() statement. Thread.Sleep(10000); will just give a delay of 10 seconds during the runtime.
- Coming back to the main application (your form), create an object of the RemoteObject class (say myRemoteObject).
- Add two buttons to the form. One for synchronous call and the other for the asynchgronous call.
- In the synchronous call button clicked event, call the GetTheMessage() method the traditional way, myRemoteObject.GetTheMessage();
- In the asynchronous call button clicked event, we will call the GetTheMessage() method asynchronous way. For this, we need a new delegate. So lets do it.
- Create a new delegate in the Form1.cs file. Lets name it AsyncCaller.
public delegate void AsyncCaller(); //Delegate for the async calling
- In the button clicked event create an object of the delegate class for the GetTheMessage() target.
AsyncCaller aAsyncCaller = new AsyncCaller(myRemoteObject.GetTheMessage()); //Ususal way of initialising a delegate method
- Lets invoke this delegate (not using .Invoke() method) using .BeginInvoke(null, null).
aAsyncCaller.BeginInvoke(null, null); //Don't care about the null and null. We will come back to that later
- Thats it. You have made an asynchronous call to the GetTheMessage() method.
Now, to test the asynchronous calls, lets run the application. After running click on the sync call button and try to drag the form using the title bar. It won’t move. The UI will be frozen until the execution of the GetTheMethod is completed. Now after seeing the message, click on the async call button and try to move the form. It will move, because the method call has been spawned to another thread. Cool, isn’t it. Lets understand the code snippet for the async calls.
- First async calls needs to be made through a delegate object. Thats why we created a delegate called AsyncCaller and a delegate method called aAsyncCaller.
- The delegate’s signature must be similar to the target’s signature (usual thingy)
- The invocation of the delegate method should be done using the BeginInvoke(); method, as it calls the method asynchronously. It automatyically creates threads and handles it. You are free from those activities.
- The BeginInvoke() method takes two parameters. CallBack and AsyncResult. Over here. WE are not concerned about the return values or the parameters of the GetTheMessage(); function. So we have null, null over there.
Note: Try to replace the MessageBox.Show(); with some other UI related statements. You should be getting an exception. We will deal about that later. In my next post, I will explain the ways to pass arguments to these asynchronous method calls.
Tags: C#, Deleagets, Tips 'n' Tricks, UI
