异步使用委托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 ...
随机推荐
- easyui时间框只选择年月
html: <input type="text" id="datetime" > js: <script type="text/ja ...
- windows powershell上批量修改文件名称
$i = Get-ChildItem -Path c:\pictures -Filter *.jpg | ForEach-Object { $extension = $_.Extension $new ...
- RandomAccessFile出现中文乱码问题
之前程序里调用了RandomAccessFile的writeByte(String str)方法,报文里存在中文的时候出现了乱码 后面换成了 write(byte b[])或writeBytes(by ...
- dubbo注册中心
官方推荐的是zookeeper注册中心. 1.Multicast 注册中心 Multicast 注册中心不需要启动任何中心节点,只要广播地址一样,就可以互相发现. 提供方启动时广播自己的地址消费方启动 ...
- spring cloud分布式配置中心案例
这里仍然以Windows.jdk和idea为开发环境,按照下面的步骤打包-运行-访问就能看到效果:启动注册中心:java -jar F:\jars-config\register-0.0.1-SNAP ...
- git(windows)
windows下比较比较好用的git客户端: 1. msysgit + TortoiseGit(乌龟git) 2. GitHub for Windows 3. Git Extensions
- pptpd免radius限速、限连接+自由定制功能脚本
因为就几个用户懒得上radius,所以手写了一个用户管理脚本. 脚本很简单,具体直接看repo吧. https://github.com/esxgx/pptpd-exscripts
- How hacker do IT: Tricks Tools and Techniques (翻译)
本资料是 Alex Noordergraaf 企业产品的说明书 现在整理如下: 第一部分: How hackers Do It : Tricks Tools and Techniques 本 ...
- mysql中null与“空值”的坑
https://blog.csdn.net/u014743697/article/details/54136092
- PHP微信商户支付 - 企业付款到零钱功能(即提现)技术资料汇总
PHP实现微信开发中提现功能(企业付款到用户零钱) 一.实现该功能目的 这几天在小程序里要实现用户从系统中提现到零钱的功能,查了一下文档可以使用 企业付款到用户零钱 来实现: 官方文档:https:/ ...