异步调用的四种方法

我们都知道普通方法运行是单线程的,如果中途有大型操作都会导致方法阻塞,表现在界面上就是,程序卡或者死掉,界面元素不动了,不响应了。C#异步调用是很好的解决方法,异步执行某个方法,程序立即开辟一个新线程去运行你的方法,主线程包括界面就不会死掉了。

  要实现C#异步调用,首先要启动C#异步调用,然后在检索C#异步调用。通常情况下,我们使用BeginInvoke方法启动C#异步调用,用EndInvoke方法检索C#异步调用结果。BeginInvoke方法与需要异步执行的方法具有相同的参数,BeginInvoke立即返回,不等待C#异步调用完成。BeginInvoke返回IasyncResult,可用于监视调用进度;EndInvoke方法之前要调用BeginInvoke,如果C#异步调用未完成,EndInvoke将一直阻塞到C#异步调用完成。EndInvoke的参数包括您需要异步执行的方法的out和ref参数以及由BeginInvoke返回的IAsyncResult。

  使用BeginInvoke和EndInvoke进行C#异步调用的常用方法

 一、使用 WaitHandle 等待异步调用

  等待 WaitHandle 是一项常用的线程同步技术。您可以使用由 BeginInvoke 返回的 IAsyncResult 的 AsyncWaitHandle 属性来获取 WaitHandle。C#异步调用完成时会发出 WaitHandle 信号,而您可以通过调用它的 WaitOne 等待它。如果您使用 WaitHandle,则在C#异步调用完成之后,但在通过调用 EndInvoke 检索结果之前,可以执行其他处理。

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

  如果启动异步调用的线程不需要处理调用结果,则可以在调用完成时执行回调方法。回调方法在 ThreadPool 线程上执行。要使用回调方法,必须将代表该方法的 AsyncCallback 委托传递给 BeginInvoke。也可以传递包含回调方法将要使用的信息的对象。

  三、轮询异步调用完成

  您可以使用由 BeginInvoke 返回的 IAsyncResult 的 IsCompleted 属性来发现C#异步调用何时完成。从用户界面的服务线程中进行C#异步调用时可以执行此操作。轮询完成允许用户界面线程继续处理用户输入。

  四、使用 EndInvoke 等待异步调用

  异步执行方法的最简单方式是以 BeginInvoke 开始,对主线程执行一些操作,然后调用 EndInvoke。EndInvoke 直到C#异步调用完成后才返回。这种技术非常适合文件或网络操作,但是由于它阻塞 EndInvoke,所以不要从用户界面的服务线程中使用它。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication2
{
    public partial class AsyncDemo : Form
    {
        public AsyncDemo()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 实现委托的方法,执行一个耗时的任务
        /// </summary>
        /// <param name="iCallTime"></param>
        /// <param name="iExecThread"></param>
        /// <returns></returns>
        string LongRunningMethod(int iCallTime, out int iExecThread)
        {
            Thread.Sleep(iCallTime);
            iExecThread = AppDomain.GetCurrentThreadId();
            return "MyCallTime was " + iCallTime.ToString();
        }

        delegate string MethodDelegate(int iCallTime, out int iExecThread);

        #region 示例 1: 同步调用方法#region 示例 1: 同步调用方法
        /**/
        /*
         *  同步调用方法
         * */
        /**/
        /// <summary>
        /// 示例 1: 同步调用方法
        /// </summary>
        public void DemoSyncCall()
        {
            string s;
            int iExecThread;

            // Create an instance of a delegate that wraps LongRunningMethod.
            MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);

            // Call LongRunningMethod using the delegate.
            s = dlgt(3000, out iExecThread);
            MessageBox.Show(AppDomain.GetCurrentThreadId().ToString());
            MessageBox.Show(string.Format("The delegate call returned the string: {0}, and the thread ID {1}", s, iExecThread.ToString()));

        }
        #endregion

        #region 示例 2: 通过 EndInvoke() 调用模式异步调用方法
        /*
         * 使用调用模式是要调用 BeginInvoke , 做某些处理主线程, 并调用 EndInvoke() 。 
         * 注意EndInvoke() 不返回直到异步调用已完成。 
         * 此调用模式是有用的,当要有调用线程正在执行异步调用, 同时工作。 
         * 有同时发生工作可改善许多应用程序的性能。 
         * 常见任务以异步运行以此方式是文件或网络操作。 
         */
        /// <summary>
        /// 示例 2: 通过 EndInvoke() 调用模式异步调用方法        
        /// </summary>
        public void DemoEndInvoke()
        {
            MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);
            string s;
            int iExecThread;

            // Initiate the asynchronous call.
            IAsyncResult ar = dlgt.BeginInvoke(5000, out iExecThread, null, null);

            // Do some useful work here. This would be work you want to have
            // run at the same time as the asynchronous call.

            // Retrieve the results of the asynchronous call.
            s = dlgt.EndInvoke(out iExecThread, ar);
            MessageBox.Show(AppDomain.GetCurrentThreadId().ToString());
            MessageBox.Show(string.Format("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString()));
        }
        #endregion

        #region 示例 3: 异步调用方法并使用 A WaitHandle 来等待调用完成
        /**/
        /*
         * 由 BeginInvoke() 返回 IAsyncResult 具有一个 AsyncWaitHandle 属性。 
         * 该属性返回 WaitHandle 异步调用完成后, 通知是。 等待 WaitHandle 是常见线程同步技术。 
         * 通过是 WaitHandle WaitOne() 方法调用线程等待 WaitHandle 上。 
         * 直到是通知 WaitHandle WaitOne() 块。 当 WaitOne() 返回, 您在调用 EndInvoke() 之前进行一些额外工作。 
         * 对于执行文件或网络操作, 否则会阻塞调用主线程存为, 以前示例中此技术很有用。
         * */
        /**/
        /// <summary>
        /// 示例 3: 异步调用方法并使用WaitHandle 来等待调用完成
        /// </summary>
        public void DemoWaitHandle()
        {
            string s;
            int iExecThread;

            MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);

            // Initiate the asynchronous call.
            IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null);

            // Do some useful work here. This would be work you want to have
            // run at the same time as the asynchronous call.

            // Wait for the WaitHandle to become signaled.
            ar.AsyncWaitHandle.WaitOne();

            // Get the results of the asynchronous call.
            s = dlgt.EndInvoke(out iExecThread, ar);

            MessageBox.Show(string.Format("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString()));
        }
        #endregion

        #region 示例 4: 异步调用方法通过轮询调用模式
        /**/
        /*
         * 由 BeginInvoke() 返回 IAsyncResult 对象有个 IsCompleted 属性异步调用完成后返回 True 。 
         * 然后可调用 EndInvoke() 。 如果您应用程序不断工作对不做要长期函数调用已被此调用模式很有用。 
         * MicrosoftWindows 应用程序是这样的示例。 
         * 主线程的 Windows 应用程序可以继续以执行异步调用时处理用户输入。 
         * 它可定期检查 IsCompleted 到调用是否完成。 它调用 EndInvoke 当 IsCompleted 返回 True 。 
         * 直到它知道操作已完成因为 EndInvoke() 阻止直到异步操作为完整, 应用程序不调用它。 
         * */
        /**/
        /// <summary>
        /// 示例 4: 异步调用方法通过轮询调用模式
        /// </summary>
        public void DemoPolling()
        {
            MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);
            string s;
            int iExecThread;

            // Initiate the asynchronous call.
            IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null);

            // Poll IAsyncResult.IsCompleted
            while (ar.IsCompleted == false)
            {
                Thread.Sleep(10);  // pretend to so some useful work
            }
            s = dlgt.EndInvoke(out iExecThread, ar);

            MessageBox.Show(string.Format("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString()));
        }
        #endregion

        #region 示例 5: 异步方法完成后执行回调
        /**/
        /*
         * 本节, 中示例提供对 BeginInvoke() 函数, 异步调用完成后系统执行回调委托。 
         * 回调调用 EndInvoke() 并处理异步调用的结果。 
         * 如果启动异步调用线程不需要处理结果是调用此调用模式很有用。 
         * 异步调用完成后系统调用线程以外启动线程上调。 
         * 若使用此调用模式, 作为第二到最后 - BeginInvoke() 函数的参数必须传递 AsyncCallback 类型的委托。 
         * BeginInvoke() 还有最后参数键入 对象 到您可以将任何对象。 当它调用该对象可用于您回调函数。 
         * 为此参数一个重要用途是以传递用于初始化调用该委托。 
         * 回调函数然后使用与该委托 EndInvoke() 函数来完成调用。 此调用模式是所示。
         * */
        /**/
        /// <summary>
        /// 示例 5: 异步方法完成后执行回调
        /// </summary>
        public void DemoCallback()
        {
            MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);
            int iExecThread;

            // Create the callback delegate.
            AsyncCallback cb = new AsyncCallback(MyAsyncCallback);

            // Initiate the Asynchronous call passing in the callback delegate
            // and the delegate object used to initiate the call.
            IAsyncResult ar = dlgt.BeginInvoke(5000, out iExecThread, cb, dlgt);
        }

        public void MyAsyncCallback(IAsyncResult ar)
        {
            string s;
            int iExecThread;

            // Because you passed your original delegate in the asyncState parameter
            // of the Begin call, you can get it back here to complete the call.
            MethodDelegate dlgt = (MethodDelegate)ar.AsyncState;

            // Complete the call.
            s = dlgt.EndInvoke(out iExecThread, ar);
            MessageBox.Show(String.Format("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString()));

            //Console.WriteLine(string.Format ("The delegate call returned the string:   "{0}", and the number {1}", s, iExecThread.ToString() ) );
        }
        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            //同步调用方法
            DemoSyncCall();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //异步调用方法并使用 A WaitHandle 来等待调用完成
            DemoWaitHandle();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //异步方法完成后执行回调
            DemoCallback();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //异步调用方法通过轮询调用模式
            DemoPolling();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //示例 2: 通过 EndInvoke() 调用模式异步调用方法 
            DemoEndInvoke();
        }
    }
}