Archive for April, 2009

Asynchronous Programming Using Delegates

April 19, 2009

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

Silverlight

April 15, 2009

While looking for a silverlight tutorial I stumbled over this blog posting it has several really use links to Silverlight Tips, Tricks, Tutorials and Misc. Links. The posting is by Scott Gutherie. I think I’ll be trying out his eight part tutorial on how to build a Digg Search Client. Now I am off to find a good WPF resource \ tutorial.

Visual Studio 2010 and C# 4.0

April 14, 2009

So I went looking for a version of C# 4.0 to download and play with and i came across this site…Visual Studio 2010 and .NET Framework 4.0 CTP

Unfortunately you need to download ~7.5 gig of virtual machine that has Visual Studio 2010 on it and it expires on 1 January 2009. The web site mentions a blog posting that show you how to work around it…Maybe I can be bothered later – but right now I will have to wait till we get it at work.

The Future of C# (Version 4.0)

April 9, 2009

I found a video from Anders Hejlsberg he heads up the language development of C# at Microsoft. There is some interesting stuff on the way with the dynamic features of the language.

There is a new data type called dynamic which is cool. There is better COM interoperability and the compiler is being opened up as a service. With the compiler being exposed you can execute and “compile” code at run-time. There is optional parameters and named parameters. And much more…

Channel9

It goes for about an hour but he (Anders Hejlsberg) is a great presenter and very charismatic.