异步使用委托delegate --- BeginInvoke和EndInvoke方法
当我们定义一个委托的时候,一般语言运行时会自动帮委托定义BeginInvoke 和 EndInvoke两个方法,这两个方法的作用是可以异步调用委托。
方法BeginInvoke有两个参数:
- AsyncCallBack:回调函数,是一个委托,没有返回值,可以传一个参数,参数类型是object;
- object AsyncState :回调函数的参数。
BeginInvoke的返回值是IAsyncResult,
方法EndInvoke需要的参数是BeginInvoke的返回值IAsyncResult.
举例如下:
class AsyncStateTest
{
private delegate void DelegateTest(); public static void TestAsyncState()
{
DelegateTest delegateTest = new DelegateTest(delegateImpl);
Console.WriteLine("Main method begin delegateTest");
IAsyncResult asyncResult = delegateTest.BeginInvoke(new AsyncCallback(CallBack), "I am AsyncState(*_*)");
//delegateTest.EndInvoke(asyncResult);
Console.WriteLine("Main method end delegateTest");
delegateTest.EndInvoke(asyncResult);
Console.WriteLine("Main method continue");
} private static void CallBack(IAsyncResult asyncResult)
{
object state = asyncResult.AsyncState;
Console.WriteLine(state.ToString());
} private static void delegateImpl()
{
Console.WriteLine("I am DelegateTest Impl");
}
}
运行结果:
Main method begin delegateTest
Main method end delegateTest
I am DelegateTest Impl
Main method continue
I am AsyncState(*_*)
可以看到,主线程的输出先于委托方法的输出,证明该方法被异步执行了。另外EndInvoke方法的作用是阻塞线程(调用委托的主线程),使之等到委托的方法执行完毕(但是不会等到委托的回调函数执行完毕),上面的代码换一下EndInvoke方法的位置可以看到结果。
异步使用委托delegate --- BeginInvoke和EndInvoke方法的更多相关文章
- 委托的BeginInvoke和EndInvoke方法
.NET Framework 允许异步调用任何方法,为了实现异步调用目标,需要定义与被调用方法具有相同签名的委托.公共语言运行时会自动使用适当的签名为该委托定义 BeginInvoke 和 EndIn ...
- [C#学习笔记之异步编程模式2]BeginInvoke和EndInvoke方法 (转载)
为什么要进行异步回调?众所周知,普通方法运行,是单线程的,如果中途有大型操作(如:读取大文件,大批量操作数据库,网络传输等),都会导致方法阻塞,表现在界面上就是,程序卡或者死掉,界面元素不动了,不响应 ...
- delegate 中的BeginInvoke和EndInvoke方法
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能 ...
- C#线程系列讲座(1):BeginInvoke和EndInvoke方法
一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能,将要执行的任务分解成多个子任务执行.这就需要在同一个进程中开启多个 ...
- [转]BeginInvoke和EndInvoke方法浅析
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提 ...
- 转:C#线程系列讲座(1) BeginInvoke和EndInvoke方法
转载自:http://www.cnblogs.com/levin9/articles/2319248.html 开发语言:C#3.0IDE:Visual Studio 2008本系列教程主要包括如下内 ...
- 黄聪:C#多线程教程(1):BeginInvoke和EndInvoke方法,解决主线程延时Thread.sleep柱塞问题(转)
开发语言:C#3.0 IDE:Visual Studio 2008 本系列教程主要包括如下内容: 1. BeginInvoke和EndInvoke方法 2. Thread类 3. 线程池 4. 线 ...
- C# BeginInvoke和EndInvoke方法
转载自:BeginInvoke和EndInvoke方法 IDE:Visual Studio 2008 本系列教程主要包括如下内容:1. BeginInvoke和EndInvoke方法 2. Threa ...
- BeginInvoke和EndInvoke方法
本系列教程主要包括如下内容:1. BeginInvoke和EndInvoke方法 2. Thread类 3. 线程池 4. 线程同步基础 5. 死锁 6. 线程同步的7种方法 7. 如何在线程中访问G ...
随机推荐
- Angular 动画
1.先做一个简单的例子 => 定义一个div 从open渐变成closed ts:定义一个触发器 openClose,有两个状态 open 和 closed,均有对应的样式,再定义装换函数 ...
- CodeForces 671C - Ultimate Weirdness of an Array
题意: 给以一个定义, F(l, r) 的值表示序列 A[1:n]的子序列 A[1....(l-1),(r+1)...n] 之中 任意两个数的最大公约数的最大值. 求 Sum=∑i=1N∑j=1N(F ...
- 迭代和JDB(课下作业,选做)
迭代和JDB(课下作业,选做) 题目要求 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 2 m,n 要通过命令行传入 3 提交测试运行截图 ...
- NOI2004郁闷的出纳员
传送门 题目看起来玄乎,但其实只需要一点点小 trick 就可以了. 我们可以用一个全局的 delta 来维护工资的调整记录 对于每一个新加入的员工,先判断是否低于最低工资下限,如果是,直接踢出,不做 ...
- win10下caffe安装与mnist测试实验注意点
caffe安装 安装内容:win10教育版+anaconda2+python(无gpu版本) 安装教程:主要依照三年一梦教程:https://www.cnblogs.com/king-lps/p/65 ...
- python3解决 json.dumps中文乱码
使用json.dumps()运行结果如下 role_name字段中文乱码了 只需要使用ensure_ascii=False 运行结果如下:
- wxpy使用
一 简介 wxpy基于itchat,使用了 Web 微信的通讯协议,,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展.实现了微信登录.收发消息.搜索好友.数据统计等功能. 总而言之,可用来实 ...
- 使用Netty实现HTTP服务器
使用Netty实现HTTP服务器,使用Netty实现httpserver,Netty Http Netty是一个异步事件驱动的网络应用程序框架用于快速开发可维护的高性能协议服务器和客户端.Netty经 ...
- PTA_输入符号及符号个数打印沙漏(C++)
思路:想将所有沙漏所需符号数遍历一遍,然后根据输入的数判断需要输出多少多少层的沙漏,然后分两部分输出沙漏. #include<iostream> #include<cstring ...
- SpringBoot主程序注解@SpringBootApplication简单分析
一.@SpringBootApplication说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用: @SpringBootA ...