In that code you specifically wait, so there is not way this isn't synchronous. You 
need to use a callback to have it be asynchronous. I modified the code so you can see 
how to do this:
 
Hope this helps,
-Bill Conroy
 
<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 DON'T poll for result
          FormattingDelegate.BeginInvoke(new 
AsyncCallback(twExecFormats.MyAsyncCallback), null);
        }
      }
    }
    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
  {
    [System.Runtime.Remoting.Messaging.OneWay]
    public static void MyAsyncCallback(IAsyncResult ar)
    {
      if (ar.IsCompleted) // should be true
      {
        Console.WriteLine("Call Done.");
      }
    }
    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");
      // without this we would just exit.
      System.Threading.Thread.Sleep(2000);
    }
  }
}

</code>

        -----Original Message----- 
        From: Mike Bluestein [mailto:[EMAIL PROTECTED]] 
        Sent: Tue 21/05/2002 14:24 
        To: [EMAIL PROTECTED] 
        Cc: 
        Subject: [DOTNET] Async Delegates
        
        

        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. 

(Ƨ杙,j~&93DM܆+޺{.n+93DN칻&޶-z++-aجr,uޖ\


Reply via email to