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
- C#——BeginInvoke & EndInvoke
- C#beginInvoke和endinvoke
- C#_delegate - 异步调用实例 BeginInvoke EndInvoke event
- BeginInvoke和EndInvoke 随笔
- BeginInvoke和EndInvoke方法
- BeginInvoke和EndInvoke方法
- BeginInvoke和EndInvoke方法
- 委托,begininvoke,endinvoke
- BeginInvoke和EndInvoke方法
- BeginInvoke和EndInvoke方法
- BeginInvoke和EndInvoke方法
- (delegate)BeginInvoke&EndInvoke
- BeginInvoke和EndInvoke方法
- BeginInvoke和EndInvoke方法
- C#之begininvoke和endinvoke
- 异步委托---使用BeginInvoke,EndInvoke
- 异步委托操作BeginInvoke和EndInvoke
- C#中的BeginInvoke和EndInvoke实现异步
- 14周作业
- SQLite Can't open — Meteor create app
- python连接SQL Server:Pymssql模块
- [金融数学] 经济学、金融学博士书目(下)
- Arbitrage poj 2240 最长路找正环
- C#——BeginInvoke & EndInvoke
- MYSQL存储引擎
- Ubuntu系统学习笔记-1.系统安装
- 面试整理(2)
- 【SSLGZ 1613】最短路径问题(Floyd)
- 分割回文串
- 使用 DS-MDK 开发 NXP iMX7
- 八皇后问题非桟的解决方法
- MyBatis+Spring分页插件配置