代码段1:
        public delegate string FuncHandle(int data1, int data2);
        FuncHandle fh ;
           
        private void BT_Temp_Click(object sender, RoutedEventArgs e)
        {
            fh = new FuncHandle(this.Foo);
            AsyncCallback callback = new AsyncCallback(this.AsyncCallbackImpl);
            fh.BeginInvoke(1, 3, callback, null);
        }

        public void AsyncCallbackImpl(IAsyncResult ar)  
        {
            string re = fh.EndInvoke(ar);
            MessageBox.Show("" + ar.AsyncState);
        }  

        string Foo(int data1, int data2)  
        {  
            return "" + data1 + data2;  
        }

在异步调用中,如果想在异步的回调函数中,得到异步函数的返回值(如上面代码中的Foo函数的string返回值),则必须要在回调函数中使用EndInvoke(关于EndInvoke会在下文描述)。在上面的例子是如下这句。
        string re = fh.EndInvoke(ar);

但是,有的时候fh并不见得是个类变量,这个时候,就需要我们将EndInvoke的执行主体由BeginInvoke传递进去。看修改过后的代码片段。

代码段2:
        public delegate string FuncHandle(int data1, int data2);  
           
        private void BT_Temp_Click(object sender, RoutedEventArgs e)
        {
            FuncHandle fh = new FuncHandle(this.Foo);
            AsyncCallback callback = new AsyncCallback(this.AsyncCallbackImpl);
            fh.BeginInvoke(1, 3, callback, fh);

        }
        public void AsyncCallbackImpl(IAsyncResult ar)  
        {  
            FuncHandle dl = ar.AsyncState as FuncHandle;  
            string re = dl.EndInvoke(ar);
            MessageBox.Show("" + ar.AsyncState);
        }  

        string Foo(int data1, int data2)  
        {  
            return "" + data1 + data2;  
        }

通过举一反三,其实BeginInvoke的最后一个参数,可以是任何对象,看具体的应用场景即可。

下面再介绍一下EndInvoke。EndInvoke在回调函数中,用于承载执行的主体函数的返回值。在另外一个情况下,即上面的代码片段一个简化版本,如下:

代码段3:
        public delegate string FuncHandle(int data1, int data2);  
        private void BT_Temp_Click(object sender, RoutedEventArgs e)
        {
            FuncHandle fh = new FuncHandle(this.Foo);
            IAsyncResult ar = fh.BeginInvoke(1, 3, null, fh);
            string re = fh.EndInvoke(ar);
            MessageBox.Show(re);
        }
       
        string Foo(int data1, int data2)  
        {  
            return "" + data1 + data2;  
        }


我们可以看到,在这个代码片段中,我们根本没有使用回调函数,那么,我们就需要通过EndInvoke来阻滞主线程,使得返回函数主体的返回值。

再多说一点,调用了 BeginInvoke 后,可以:

1.进行某些操作,然后调用 EndInvoke 一直阻塞到调用完成。如上文的最后一个代码片段。

2.使用 IAsyncResult.AsyncWaitHandle 获取 WaitHandle,使用它的 WaitOne 方法将执行一直阻塞到发出 WaitHandle 信号,然后调用EndInvoke。这里主要是主程序等待异步方法,等待异步方法的结果。见代码段4。

3.轮询由 BeginInvoke 返回的 IAsyncResult,IAsyncResult.IsCompeted确定异步调用何时完成,然后调用 EndInvoke。

4.将用于回调方法的委托传递给 BeginInvoke。该方法在异步调用完成后在 ThreadPool 线程上执行,它可以调用 EndInvoke。这是在强制装换回调函数里面IAsyncResult.AsyncState(BeginInvoke方法的最后一个参数)成委托,然后用委托执行EndInvoke。即如上代码片段2。

代码段4:
        public delegate string FuncHandle(int data1, int data2);
        string _sTemp = string.Empty;

        private void BT_Temp_Click(object sender, RoutedEventArgs e)
        {
            FuncHandle fh = new FuncHandle(this.Foo);
            AsyncCallback callback = new AsyncCallback(this.AsyncCallbackImpl);
            IAsyncResult ar = fh.BeginInvoke(1, 3, null, null);
            WaitHandle waitHandle = ar.AsyncWaitHandle;
            waitHandle.WaitOne();
            MessageBox.Show(_sTemp);
        }

        string Foo(int data1, int data2)  
        {
            Thread.Sleep(3000);
            string re = "" + data1 + data2;
            _sTemp = re;
            return re;
        }