异步使用委托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 ...
随机推荐
- 基于Python的Webservice开发(一)-简介
之前为了解决Webservice的开发,直接用Python自带的CGI模块挂在IIS上. 但是该方式开发Soap的接口,需要大量的开发,而且安全方面也存在很多问题. 我推荐关于用Python开发Web ...
- javascript闭包学习
(function(){})()===>>>>函数会被立即执行function(){}是一个函数用括号包起来表示是函数表达式再加()表示函数自执行 如何理解闭包?1.定义和用 ...
- jmeter创建时间函数
固定格式的年月日 ${__time(yyyyMMdd,)} 20151214 //返回年月日 ${__time(HHmmss,)} 092816 //返回时分秒 ${__time(yyyyMMdd-H ...
- 【Python】Flask中@wraps的使用
先说总结,白话来讲,@wraps相当于是装饰器的装饰器. python内置的方法使用解释,看起很复杂的样子┓( ´∀` )┏ def wraps(wrapped, assigned = WRAPPER ...
- 基于Spring Security OAuth2搭建的Spring Cloud 认证中心
Github传送门:https://github.com/13babybear/bounter-springcloud 实现功能有: 整合JWT 刷新Token 自定义客户端储存 自定义用户储存 资源 ...
- sqlite3 学习
安装sqlite: [ sqlite]$ wget http://www.sqlite.org/sqlite-3.6.16.tar.gz[ sqlite]$ tar -xzvf sqlite-3.6. ...
- windows10中微软小娜cortana如何彻底卸载删除?
windows10中的Cortana可以通过语音干很多事情,但是对于我们来说用处不大,而且开机十分占用内存,下面教大家如何彻底的卸载并删除: 首先下载卸载Cortana的软件,下载链接:http:// ...
- pip的安装问题
1. pip install 要不要加 sudo 不加sudo经常会遇到权限问题,如: $ pip install robotframeworklexer Collecting robotframew ...
- urlparse解析URL参数
python2 #! /usr/bin/env python # -*- coding:utf8 -*- # Author:zhangning import urlparse def url2Dict ...
- No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述: No enclosing instance of type Test is accessibl ...