C#--委托的同步,异步,回调函数

原文地址异步

同步调用

委托的Invoke方法用来进行同步调用。同步调用也能够叫阻塞调用,它将阻塞当前线程,而后执行调用,调用完毕后再继续向下进行。函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace invoke
{
    public delegate int AddHandler(int a, int b);
    public class 加法类
    {
        public static int Add(int a, int b)
        {
            Console.WriteLine("开始计算:" + a + "+" + b);
            Thread.Sleep(3000); //模拟该方法运行三秒
            Console.WriteLine("计算完成!");
            return a + b;
        }
    }

    public class 同步调用
    {
        static void Main()
        {
            Console.WriteLine("===== 同步调用 SyncInvokeTest =====");
            AddHandler handler = new AddHandler(加法类.Add);
            int result = handler.Invoke(1, 2);

            Console.WriteLine("继续作别的事情。。。");
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}

异步调用

异步调用不阻塞线程,而是把调用塞到线程池中,程序主线程或UI线程能够继续执行。委托的异步调用经过BeginInvoke和EndInvoke来实现。spa

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace invoke
{

    public class 异步调用
    {
        static void Main2()
        {
            Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
            AddHandler handler = new AddHandler(加法类.Add);
            //IAsyncResult: 异步操做接口(interface)
            //BeginInvoke: 委托(delegate)的一个异步方法的开始
            IAsyncResult result = handler.BeginInvoke(1, 2, null, null);
            Console.WriteLine("继续作别的事情。。。");
            //异步操做返回
            Console.WriteLine(handler.EndInvoke(result));
            Console.ReadKey();
        }
    }
}

异步回调

用回调函数,当调用结束时会自动调用回调函数,能够在回调函数里触发EndInvoke,这样就能够避免程序一直占用一个线程,就释放掉了线程.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace invoke
{
    public class 异步回调
    {
        static void Main3()
        {
            Console.WriteLine("===== 异步回调 AsyncInvokeTest =====");
            AddHandler handler = new AddHandler(加法类.Add);
            //异步操做接口(注意BeginInvoke方法的不一样!)
            IAsyncResult result = handler.BeginInvoke(1, 2, new AsyncCallback(回调函数), "AsycState:OK,这里输出的是AsyncState的参数内容");
            Console.WriteLine("继续作别的事情。。。");
            Console.ReadKey();
        }
        static void 回调函数(IAsyncResult result)
        {
            //result 是“加法类.Add()方法”的返回值
            //AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging
            //AsyncDelegate 属性能够强制转换为用户定义的委托的实际类。
            AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
            Console.WriteLine(handler.EndInvoke(result));
            Console.WriteLine(result.AsyncState);
        }
    }
}

代码下载线程

相关文章
相关标签/搜索