Below is the example that I mercilessly copied from Jon's article. In
this example, I do not understand how his AsyncResult was able to
retrieve delegates. In other words, using AsyncResult delegateResult
= (AsyncResult) result --will it always return the active/completed
delegates in loaded in CLR?
Thanks
using System;
using System.Threadin g;
using System.Runtime. Remoting.Messag ing;
delegate int SampleDelegate( string data);
class AsyncDelegateEx ample2
{
static void Main()
{
SampleDelegate counter = new SampleDelegate( CountCharacters );
SampleDelegate parser = new SampleDelegate( Parse);
AsyncCallback callback = new AsyncCallback (DisplayResult) ;
counter.BeginIn voke ("hello", callback, "Counter returned
{0}");
parser.BeginInv oke ("10", callback, "Parser returned {0}");
Console.WriteLi ne ("Main thread continuing");
Thread.Sleep (3000);
Console.WriteLi ne ("Done");
}
static void DisplayResult(I AsyncResult result)
{
string format = (string) result.AsyncSta te;
AsyncResult delegateResult = (AsyncResult) result;
SampleDelegate delegateInstanc e =
(SampleDelegate )delegateResult .AsyncDelegate;
Console.WriteLi ne (format,
delegateInstanc e.EndInvoke(res ult));
}
static int CountCharacters (string text)
{
Thread.Sleep (2000);
Console.WriteLi ne ("Counting characters in {0}", text);
return text.Length;
}
static int Parse (string text)
{
Thread.Sleep (100);
Console.WriteLi ne ("Parsing text {0}", text);
return int.Parse(text) ;
}
}