Team LiB
Previous Section Next Section

Making Asynchronous Calls

By default, all delegates support synchronous calls. This is built in, so you do not need to do anything special. To make asynchronous calls, you use the BeginInvoke method of the delegate. It is important to realize that all asynchronous calls are handled by the ThreadPool, so you do not have the option of canceling these calls.

The following examples demonstrate making asynchronous calls and an asynchronous wait timeout.

Code Example: Asynchronous Calls Using Callback
Start example
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace Client.Chapter_15___Threading
{
          class MyMainClass
          {
                 delegate int MyDelegate(string s, ref int a, ref int b);
                 static void Main(string[] args)
                 {
                        MyDelegate X = new MyDelegate(DoSomething);
                        int a = 0;
                        int b = 0;
                       //Make async call that calls a callback when finished
                        AsyncCallback cb = new AsyncCallback(DoSomething2);
                       IAsyncResult ar =
                       X.BeginInvoke("Hello", ref a, ref b, cb, null);
                        Console.ReadLine();
                 }
                //My async method
                 static int DoSomething(string s, ref int a, ref int b)
                 {
                        a = 10;
                        b = 100;
                        Console.WriteLine("Fired! DoSomething1");
                        return 0;
                 }
                //My callback method when finished running DoSomething
                 static void DoSomething2(IAsyncResult ar)
                 {
                        int a = 0;
                        int b = 0;
                        Console.WriteLine("Fired! DoSomething2");
                       //Get the delegate
                       MyDelegate X = (MyDelegate)((AsyncResult)ar).AsyncDelegate;
                      //Get results
                       X.EndInvoke(ref a, ref b, ar);
                       Console.WriteLine(a);
                       Console.WriteLine(b);
                 }
          }
}
End example
Code Example: Asynchronous Wait Timeout
Start example

using System;
using System.Runtime.Remoting.Messaging;

namespace Client.Chapter_15___Threading
{
          class MyMainClass
          {
                  delegate int MyDelegate(string s, ref int a, ref int b);
                  static void Main(string[] args)
                  {
                          MyDelegate X = new MyDelegate(DoSomething);
                          int a = 0;
                          int b = 0;
                          IAsyncResult ar = X.BeginInvoke("Hello", ref a, ref b,
                          null, null);
                          ar.AsyncWaitHandle.WaitOne(10000, false);
                          if (ar.IsCompleted)
                          {
                               int c = 0;
                               int d = 0;
                             //Get results
                              X.EndInvoke(ref c, ref d, ar);
                              Console.WriteLine(c);
                              Console.WriteLine(d);
                          }
                  }
                 //My async method
                  static int DoSomething(string s, ref int a, ref int b)
                  {
                            a = 10;
                            b = 100;
                            Console.WriteLine("Fired! DoSomething1");
                            return 0;
                  }
          }
}
End example

Team LiB
Previous Section Next Section