I'm trying to implement a simple command line program to make an asynchronus method call.
The calls all seem to work but they appear to be synchronous. In the code below, I was expect to see the Console echo the text "before call" and "after call" prior to the method called by the async delegate completing. However, the "after call" doesn't seem to write until after the async stuff finishes. So, this looks to me to be doing nothing more that a synchronous call would. What am I doing wrong here? Thanks.... Here's the code: using System; namespace AsyncFormatsNET { delegate void Working(); class twFormats { public void Format() { //Sync Call - IL calls Invoke() method of delegate //if (OnFormatting != null) OnFormatting(); //Async call - BeginInvoke() method of delegate if (OnFormatting != null) { foreach(Working FormattingDelegate in OnFormatting.GetInvocationList()) { //fire and poll for result IAsyncResult AsyncProxyResult = FormattingDelegate.BeginInvoke (null,null); while(!AsyncProxyResult.IsCompleted) { Console.WriteLine("Asychronous call to twFormatsProxy.CallProxy NOT completed yet"); System.Threading.Thread.Sleep(1000); } FormattingDelegate.EndInvoke(AsyncProxyResult); Console.WriteLine("Asychronous call to twFormatsProxy.CallProxy completed"); } } } public event Working OnFormatting; } class twFormatsProxyInterop { public void CallProxy() { //To Do: add proxy call Console.WriteLine("Proxy called"); //Simulate a 5 second proxy call System.Threading.Thread.Sleep(5000); } } //Client class twExecFormats { static void Main() { twFormats twf = new twFormats(); twFormatsProxyInterop twi = new twFormatsProxyInterop(); twf.OnFormatting += new Working(twi.CallProxy); Console.WriteLine("Before call"); twf.Format(); /*I was expected this line to fire and the have exectution proceed immediately to the next line, but the code seems to get stuck here util the call completes???????*/ Console.WriteLine("After call"); } } } You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.