CreateCard crar = new CreateCard(rar.CreateTxt); 
IAsyncResult ar = crar.BeginInvoke(num, new AsyncCallback(CreateRar), snum);
public static void CreateRar(IAsyncResult ar)
    {
       
    }上面是委托的异步调用怎么把参数传给回调函数啊,象我上面那样写对不对?snum是否能传给
CreateRar作为参数啊那在CreateRar怎么接受这个参数呢

解决方案 »

  1.   

    还有,回调函数能否写成private的?
      

  2.   

    private delegate void CreateCard(string num);是我定义的委托啊
      

  3.   

    还有,回调函数能否写成private的?
    ========================
    关键看是谁在访问这个回调
      

  4.   

    用IAsyncResult.State来保存你需要的数据
    IAsyncResult ar = crar.BeginInvoke(num, new AsyncCallback(CreateRar), snum);
    ar.State = snum;public static void CreateRar(IAsyncResult ar)
    {
      int num = (int)ar.State;
      ......
    }
      

  5.   

    异步委托提供以异步方式调用同步方法的能力。当同步调用一个委托时,“Invoke”方法直接对当前线程调用目标方法。如果编译器支持异步委托,则它将生成“Invoke”方法以及“BeginInvoke”和“EndInvoke”方法。如果调用“BeginInvoke”方法,则公共语言运行库 (CLR) 将对请求进行排队并立即返回到调用方。将对来自线程池的线程调用该目标方法。提交请求的原始线程自由地继续与目标方法并行执行,该目标方法是对线程池线程运行的。如果在对“BeginInvoke”方法的调用中指定了回调方法,则当目标方法返回时将调用该回调方法。在回调方法中,“EndInvoke”方法获取返回值和所有输入/输出参数。如果在调用“BeginInvoke”时未指定任何回调方法,则可以从调用“BeginInvoke”的线程中调用“EndInvoke”。要点 
    使用用户指定的委托签名,编译器应发出具有“Invoke”、“BeginInvoke”和“EndInvoke”方法的委托类。“BeginInvoke”和“EndInvoke”方法应被修饰为本机的。因为这些方法被标记为本机的,所以 CLR 在类加载时自动提供该实现。加载程序确保它们未被重写。
     
    using System;
    using System.Threading;
    using System.Runtime.Remoting.Messaging;namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        // Create a class that factors a number.
        public class PrimeFactorFinder
        {
           public static bool Factorize(
                        int number,
                        ref int primefactor1,
                        ref int primefactor2)
           {
              primefactor1 = 1;
              primefactor2 = number;          // Factorize using a low-tech approach.
              for (int i=2;i<number;i++)
              {
                 if (0 == (number % i))
                 {
                    primefactor1 = i;
                    primefactor2 = number / i;
                    break;
                 }
              }
              if (1 == primefactor1 )
                 return false;
              else
                 return true   ;
           }
        }    // Create an asynchronous delegate that matches the Factorize method.
        public delegate bool AsyncFactorCaller (
                 int number, 
                 ref int primefactor1,
                 ref int primefactor2);    public class DemonstrateAsyncPattern
        {
            // The waiter object used to keep the main application thread
            // from terminating before the callback method completes.
            ManualResetEvent waiter;
            
            // Define the method that receives a callback when the results are available.
            public void FactorizedResults(IAsyncResult result)
               {
                  int factor1=0;
                  int factor2=0; 
            
                  // Extract the delegate from the 
                  // System.Runtime.Remoting.Messaging.AsyncResult.
                  AsyncFactorCaller factorDelegate = (AsyncFactorCaller)((AsyncResult)result).AsyncDelegate;
                  int number = (int) result.AsyncState;
                  // Obtain the result.
                  bool answer = factorDelegate.EndInvoke(ref factor1, ref factor2, result);
                  // Output the results.
                  Console.WriteLine("On CallBack: Factors of {0} : {1} {2} - {3}", 
                      number, factor1, factor2, answer);
                  waiter.Set();
               }       // The following method demonstrates the asynchronous pattern using a callback method.
           public void FactorizeNumberUsingCallback()
           {
              AsyncFactorCaller factorDelegate = new AsyncFactorCaller (PrimeFactorFinder.Factorize);
              int number = 1000589023;
              int temp=0; 
              // Waiter will keep the main application thread from 
              // ending before the callback completes because
              // the main thread blocks until the waiter is signaled
              // in the callback.
               waiter = new ManualResetEvent(false);          // Define the AsyncCallback delegate.
              AsyncCallback callBack = new AsyncCallback(this.FactorizedResults);          // Asynchronously invoke the Factorize method.
              IAsyncResult result = factorDelegate.BeginInvoke(
                                   number, 
                                   ref temp, 
                                   ref temp, 
                                   callBack, 
                                   number);           // Do some other useful work while 
              // waiting for the asynchronous operation to complete.          // When no more work can be done, wait.
              waiter.WaitOne();
           }       // The following method demonstrates the asynchronous pattern 
           // using a BeginInvoke, followed by waiting with a time-out.
           public void FactorizeNumberAndWait()
           {
              AsyncFactorCaller factorDelegate = new AsyncFactorCaller (PrimeFactorFinder.Factorize);          int number = 1000589023;
              int temp=0;           // Asynchronously invoke the Factorize method.
              IAsyncResult result = factorDelegate.BeginInvoke(
                                number, 
                                ref temp, 
                                ref temp, 
                                null, 
                                null); 
                                
              while (!result.IsCompleted)
              {
                // Do any work you can do before waiting.
                result.AsyncWaitHandle.WaitOne(10000, false);
              }
              // The asynchronous operation has completed.
              int factor1=0;
              int factor2=0;          // Obtain the result.
             bool answer = factorDelegate.EndInvoke(ref factor1, ref factor2, result);         // Output the results.
             Console.WriteLine("Sequential : Factors of {0} : {1} {2} - {3}", 
                               number, factor1, factor2, answer);
           }       public static void Main()
           {
              DemonstrateAsyncPattern demonstrator = new DemonstrateAsyncPattern();
              demonstrator.FactorizeNumberUsingCallback();
              demonstrator.FactorizeNumberAndWait();
           }
        }
    }
      

  6.   

    IAsyncResult ar = crar.BeginInvoke(num, new AsyncCallback(CreateRar), snum);
    这里的第三个参数是什么啊?如果回调函数都是在本类里,我可以写成private类型的吗?