C#——BeginInvoke & EndInvoke

来源:互联网 发布:全球数据库 编辑:程序博客网 时间:2024/05/21 05:40

委托的异步调用


【引用】

C#——委托



委托有个调用列表,当调用委托的时候,里面的所有注册函数都会调用一遍,而且是同步顺序执行的。

如果委托里面只有一个注册函数的时候,我们就可以通过BeginInvoke和EnvInvoke实现委托的异步调用。

特点:
1.主线程在BeginInvoke的时候会开一个线程去跑委托里的内容,同时返回一个IAsyncResult类型的返回值
2.EndInvoke会等待新开线程运行结束,然后进行线程清理工作,并且返回委托调用的返回值。
3.BeginInvoke和EndInvoke要成对使用

等待结束模式

using System;using System.Threading;namespace InvokeTest{    delegate long MyDel( int FirstParam, int SecondParam );    class Program    {        static long Sum( int x, int y )        {            Console.WriteLine( "   in the sum process" );            Thread.Sleep(100);            return x + y;        }        static void Main()        {            MyDel del = new MyDel( Sum );            Console.WriteLine("Before BeginInvoke");            IAsyncResult IResult = del.BeginInvoke( 3, 4, null, null );            Console.WriteLine("End BeginInvoke");            Console.WriteLine("Do something in main process");            long lResult = del.EndInvoke( IResult );            Console.WriteLine( "After EndInvoke: {0}", lResult );        }    }}

运行结果:
这里写图片描述


轮询模式

using System;using System.Threading;namespace InvokeTest{    delegate long MyDel( int FirstParam, int SecondParam );    class Program    {        static long Sum( int x, int y )        {            Console.WriteLine( "   in the sum process" );            Thread.Sleep(100);            return x + y;        }        static void Main()        {            MyDel del = new MyDel( Sum );            Console.WriteLine("Before BeginInvoke");            IAsyncResult IResult = del.BeginInvoke( 3, 4, null, null );            Console.WriteLine("End BeginInvoke");            // 开启轮询,不断的查看IResult的完成属性            while( !IResult.IsCompleted )            {                Console.WriteLine( "Not Done" );                // 其他处理                Thread.Sleep(10);            }            Console.WriteLine( "Done" );            long lResult = del.EndInvoke( IResult );            Console.WriteLine( "After EndInvoke: {0}", lResult );        }    }}

轮询结果:
这里写图片描述


回调模式

using System;using System.Threading;using System.Runtime.Remoting.Messaging;   //AsyncResult类型的声明namespace InvokeTest{    delegate long MyDel( int FirstParam, int SecondParam );    class Program    {        static long Sum( int x, int y )        {            Console.WriteLine( "   in the sum process" );            Thread.Sleep(100);            return x + y;        }        // 异步调用完成以后的回调函数        static void CallBackWhenDone( IAsyncResult IAResult )        {            Console.WriteLine( "      Inside Callback" );            AsyncResult AResult = (AsyncResult)IAResult;            MyDel del = (MyDel)AResult.AsyncDelegate;            long lResult = del.EndInvoke( IAResult );            Console.WriteLine( "The result is {0}", lResult );        }        static void Main()        {            MyDel del = new MyDel( Sum );            Console.WriteLine("Before BeginInvoke");            IAsyncResult IResult = del.BeginInvoke( 3, 4, CallBackWhenDone, null );            Console.WriteLine("End BeginInvoke");            Console.WriteLine( "Do somting" );            Thread.Sleep(200);            Console.WriteLine( "All Done" );        }    }}

运行结果:
这里写图片描述

0 0
原创粉丝点击