日期:2014-05-19  浏览次数:20802 次

关于EndInvoke
这是一个MSDN上的一个简单的代码,熟悉的可以不看代码
我弄不懂的是EndInvoke方法的参数ThreadId,result,这两个参数有什么用,该方法返回的的值是不是异步调用的方法的返回值,EndInvoke方法的参数个数取决于什么??????????
using   System;
using   System.Threading;
namespace   Examples.AdvancedProgramming.AsynchronousOperations
{
        public   class   AsyncDemo
        {
                public   string   TestMethod(int   callDuration,   out   int   threadId)
                {
                        Console.WriteLine( "Test   method   begins. ");
                        Thread.Sleep(callDuration);
                        threadId   =   Thread.CurrentThread.ManagedThreadId;
                        return   String.Format( "My   call   time   was   {0}. ",   callDuration.ToString());
                }

                public   delegate   string   AsyncMethodCaller(int   callDuration,   out   int   threadId);
                public   static   void   Main()
                {
                        int   threadId;
                        AsyncDemo   ad   =   new   AsyncDemo();
                        AsyncMethodCaller   caller   =   new   AsyncMethodCaller(ad.TestMethod);
                        IAsyncResult   result   =   caller.BeginInvoke(3000,
                                out   threadId,   null,   null);
                        Thread.Sleep(0);
                        Console.WriteLine( "Main   thread   {0}   does   some   work. ",
                                Thread.CurrentThread.ManagedThreadId);
                        string   returnValue   =   caller.EndInvoke(out   threadId,   result);//????????
                        Console.WriteLine( "The   call   executed   on   thread   {0},   with   return   value   \ "{1}\ ". ",
                                threadId,   returnValue);
                        Console.ReadLine();