Asynchronous Programming Using Delegates

Here is the most basic use of invocation of a delegate.

   1: using System;

   2: using System.Threading;

   3:  

   4: namespace Delegates1   

   5: {   

   6:     class Program   

   7:     {   

   8:         delegate int sleepTime( int time );

   9:         public int sleepTimeImpl(int time)

  10:         {

  11:             Console.WriteLine("Enter Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  12:             Thread.Sleep(time);

  13:             Console.WriteLine("Exiting Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  14:             return time / 1000;

  15:         }

  16:         static void Main(string[] args)

  17:         {

  18:             Program p = new Program();

  19:             sleepTime st = p.sleepTimeImpl;

  20:             Console.WriteLine("Program took {0} seconds.", st.Invoke(1000));

  21:             Console.ReadLine();

  22:         }

  23:     }

  24: }

Next you can call BeginInvoke and EndInvoke to call the method asynchronously.

   1: using System;

   2: using System.Threading;

   3:  

   4: namespace Delegates1

   5: {

   6:     class Program

   7:     {

   8:         delegate int sleepTime( int time );

   9:         public int sleepTimeImpl(int time)

  10:         {

  11:             Console.WriteLine("Enter Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  12:             Thread.Sleep(time);

  13:             Console.WriteLine("Exiting Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  14:             return time / 1000;

  15:         }

  16:         static void Main(string[] args)

  17:         {

  18:             Program p = new Program();

  19:             sleepTime st = p.sleepTimeImpl;

  20:             IAsyncResult result = st.BeginInvoke(3000, null, null);

  21:             Console.WriteLine("Called Method.");

  22:             while (!result.IsCompleted)

  23:             {

  24:                 Console.WriteLine("Waiting.");

  25:                 result.AsyncWaitHandle.WaitOne(500, false);

  26:             }

  27:             Console.WriteLine("Program took {0} seconds.", st.EndInvoke(result));

  28:             Console.ReadLine();

  29:         }

  30:     }

  31: }

But to do several task at the same time and use a delegate asynchronously do the following…

   1: using System;

   2: using System.Threading;

   3:  

   4: namespace Delegates1

   5: {

   6:     class Program

   7:     {

   8:         delegate int sleepTime( int time );

   9:         public int sleepTimeImpl(int time)

  10:         {

  11:             Console.WriteLine("Enter Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  12:             Thread.Sleep(time);

  13:             Console.WriteLine("Exiting Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  14:             return time / 1000;

  15:         }

  16:         static void Main(string[] args)

  17:         {

  18:             const int arraySize = 6;

  19:             int[] sleepArray = new int[arraySize] { 10000, 9000, 12000, 18000, 16000, 6000 };

  20:             IAsyncResult[] iarArray = new IAsyncResult[arraySize];

  21:             Program program = new Program();

  22:             sleepTime st = program.sleepTimeImpl;

  23:             int i = 0;

  24:             foreach (int sleepItem in sleepArray)

  25:             {

  26:                 iarArray[i++] = st.BeginInvoke(sleepItem, null, null);

  27:             }

  28:             int counter = 0;

  29:             while (counter != arraySize)

  30:             {

  31:                 int current = 0;

  32:                 foreach (IAsyncResult iar in iarArray)

  33:                 {

  34:                     if (iar != null && iar.IsCompleted)

  35:                     {

  36:                         Console.WriteLine("Program took {0} seconds.", st.EndInvoke(iar));

  37:                         iarArray[current] = null;

  38:                         counter++;

  39:                     }

  40:                     current++;

  41:                 }

  42:             }

  43:             Console.ReadLine();

  44:         }

  45:     }

  46: }

 
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s


%d bloggers like this: