C#使用异步方式调用同步方法

http://msdn.microsoft.com/zh-cn/library/2e08f6yc.aspx

异步调用完成时执行回调方法

如果启动异步调用的线程不是需要处理结果的线程,则可以在调用完成时执行回调方法。 回调方法在 ThreadPool 线程上执行。

若要使用回调方法,必须将表示回调方法的 AsyncCallback 委托传递给 BeginInvoke 也可以传递包含回调方法要使用的信息的对象。 在回调方法中,可以将 IAsyncResult(回调方法的唯一参数)强制转换为 AsyncResult 对象。 然后,可以使用 AsyncResult.AsyncDelegate 属性获取已用于启动调用的委托,以便可以调用 EndInvoke

有关示例的说明:

  • TestMethod 的 threadId 参数为 out 参数(在 Visual Basic 中为 <Out> ByRef),因此 TestMethod 从不使用该参数的输入值。 会将一个虚拟变量传递给 BeginInvoke 调用。 如果 threadId 参数为 ref 参数(在 Visual Basic 中为 ByRef),则该变量必须为类级别字段,这样才能同时传递给BeginInvoke 和 EndInvoke

  • 传递给 BeginInvoke 的状态信息是一个格式字符串,回调方法使用该字符串来设置输出消息的格式。 因为作为类型 Object 进行传递,所以状态信息必须强制转换为正确的类型才能被使用。

  • 回调在 ThreadPool 线程上执行。 ThreadPool 线程是后台线程,这些线程不会在主线程结束后保持应用程序的运行,因此示例的主线程必须休眠足够长的时间以便回调完成。


    using System;
    
    using System.Threading;
    
    using System.Runtime.Remoting.Messaging;
    
    
    
    namespace Examples.AdvancedProgramming.AsynchronousOperations
    
    {
    
        public class AsyncMain 
    
        {
    
            static void Main() 
    
            {
    
                // Create an instance of the test class.
    
                AsyncDemo ad = new AsyncDemo();
    
    
    
                // Create the delegate.
    
                AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
    
    
    
                // The threadId parameter of TestMethod is an out parameter, so
    
                // its input value is never used by TestMethod. Therefore, a dummy
    
                // variable can be passed to the BeginInvoke call. If the threadId
    
                // parameter were a ref parameter, it would have to be a class-
    
                // level field so that it could be passed to both BeginInvoke and 
    
                // EndInvoke.
    
                int dummy = 0;
    
    
    
                // Initiate the asynchronous call, passing three seconds (3000 ms)
    
                // for the callDuration parameter of TestMethod; a dummy variable 
    
                // for the out parameter (threadId); the callback delegate; and
    
                // state information that can be retrieved by the callback method.
    
                // In this case, the state information is a string that can be used
    
                // to format a console message.
    
                IAsyncResult result = caller.BeginInvoke(3000,
    
                    out dummy, 
    
                    new AsyncCallback(CallbackMethod),
    
                    "The call executed on thread {0}, with return value \"{1}\".");
    
    
    
                Console.WriteLine("The main thread {0} continues to execute...", 
    
                    Thread.CurrentThread.ManagedThreadId);
    
    
    
                // The callback is made on a ThreadPool thread. ThreadPool threads
    
                // are background threads, which do not keep the application running
    
                // if the main thread ends. Comment out the next line to demonstrate
    
                // this.
    
                Thread.Sleep(4000);
    
    
    
                Console.WriteLine("The main thread ends.");
    
            }
    
    
    
            // The callback method must have the same signature as the
    
            // AsyncCallback delegate.
    
            static void CallbackMethod(IAsyncResult ar) 
    
            {
    
                // Retrieve the delegate.
    
                AsyncResult result = (AsyncResult) ar;
    
                AsyncMethodCaller caller = (AsyncMethodCaller) result.AsyncDelegate;
    
    
    
                // Retrieve the format string that was passed as state 
    
                // information.
    
                string formatString = (string) ar.AsyncState;
    
    
    
                // Define a variable to receive the value of the out parameter.
    
                // If the parameter were ref rather than out then it would have to
    
                // be a class-level field so it could also be passed to BeginInvoke.
    
                int threadId = 0;
    
    
    
                // Call EndInvoke to retrieve the results.
    
                string returnValue = caller.EndInvoke(out threadId, ar);
    
    
    
                // Use the format string to format the output message.
    
                Console.WriteLine(formatString, threadId, returnValue);
    
            }
    
        }
    
    }
    
    
    
    /* This example produces output similar to the following:
    
    
    
    The main thread 1 continues to execute...
    
    Test method begins.
    
    The call executed on thread 3, with return value "My call time was 3000.".
    
    The main thread ends.
    
     */
    
    
    
    

     

 

你可能感兴趣的:(C#)