(二)异步方法BeginInvoke和EndInvoke
.Net framework可以让你异步调用任何方法,你可以定义一个与你要调用的方法的签名相同的委托。公共语言运行时将自动为该委托定义与签名相同的BeginInvok和EndInvoke方法。
BeginInvoke方法触发你的异步方法,它和你想要执行的异步方法有相同的参数。另外还有两个可选参数,第一个是AsyncCallback委托是异步完成的回调方法。第二个是用户自定义对象,该对象将传递到回调方法中。BeginInvoke立即返回并且不等待完成异步的调用(继续执行该下面的代码,不需要等待)。BeginInvoke返回IAsyncResult接口,可用于检测异步调用的过程。
通过EndInvoke方法检测异步调用的结果。如果异步调用尚未完成,EndInvoke将阻塞调用线程,直到它完成
下面代码演示使用BeginInvoke和EndInvoke进行异步调用的四种常见方式。在调用BeginInvoke可以做以下工作:
- 做一些其他操作,然后调用EndInvoke方法阻塞线程直到该方法完成。
- 使用IAsyncResult.AsyncWaitHandle属性,使用它的WaitOne方法阻塞线程直到收到WaitHandle信号,然后调用EndInvoke。
- 检查BeginInvoke返回值IAsyncResult的状态来决定方法是否完成,然后调用EndInvoke方法。
public delegate string AsyncMethodCaller(int callDuration, out int threadId);
class Program
{
static void Main(string[] args)
{ }
static string TestMethod(int callDuration, out int threadId)
{
Console.WriteLine("Test method begins:");
//睡一会 模拟耗时操作
Thread.Sleep(callDuration);
threadId = Thread.CurrentThread.ManagedThreadId;
return string.Format("My call time was {0}.", callDuration.ToString());
}
}
情况一:通过EndInovke阻塞线程,直到异步调用结束。
static void Main(string[] args)
{
int threadId;
AsyncMethodCaller caller = new AsyncMethodCaller(TestMethod);
IAsyncResult result = caller.BeginInvoke(, out threadId, null, null);
//调用EndInvoke方法,等待异步嗲用完成,并得到结果。
string returnValue = caller.EndInvoke(out threadId, result);
Console.WriteLine("The call executed on thread {0},with return value {1}.", threadId, returnValue);
Console.Read();
}
情况二:通过WaitHandle属性阻塞线程。
static void Main(string[] args)
{
int threadId;
AsyncMethodCaller caller = new AsyncMethodCaller(TestMethod);
IAsyncResult result = caller.BeginInvoke(, out threadId, null, null);
Thread.Sleep();
Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);
//等待WaitHandle信号
result.AsyncWaitHandle.WaitOne();
//调用EndInvoke方法,等待异步嗲用完成,并得到结果。
string returnValue = caller.EndInvoke(out threadId, result);
//关闭等待句柄
result.AsyncWaitHandle.Close();
Console.WriteLine("The call executed on thread {0},with return value {1}.", threadId, returnValue);
Console.Read();
}
情况三:检查BeginInvoke返回结果的状态。
可以通过BeginInvoke的返回结果的IsCompleted属性检查异步是否完成。你可以在异步没有完成的时候做其他的操作。
static void Main(string[] args)
{
int threadId;
AsyncMethodCaller caller = new AsyncMethodCaller(TestMethod);
IAsyncResult result = caller.BeginInvoke(, out threadId, null, null);
Thread.Sleep();
Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);
while (result.IsCompleted == false)
{
Thread.Sleep();
Console.WriteLine("*");
}
//调用EndInvoke方法,等待异步嗲用完成,并得到结果。
string returnValue = caller.EndInvoke(out threadId, result);
//关闭等待句柄
result.AsyncWaitHandle.Close();
Console.WriteLine("The call executed on thread {0},with return value {1}.", threadId, returnValue);
Console.Read();
}
情况4、可以使用回调函数。
private delegate string RunOnThreadPool(out int threadId);
private static void Callback(IAsyncResult ar)
{
Console.WriteLine("开始回调方法");
Console.WriteLine("回调方法的参数: {0}", ar.AsyncState);
Console.WriteLine("是否线程池: {0}", Thread.CurrentThread.IsThreadPoolThread);
Console.WriteLine("线程池线程 id: {0}", Thread.CurrentThread.ManagedThreadId);
}
private static string Test(out int threadId)
{
Console.WriteLine("Starting...");
Console.WriteLine("是否线程池: {0}", Thread.CurrentThread.IsThreadPoolThread);
Thread.Sleep(TimeSpan.FromSeconds());
threadId = Thread.CurrentThread.ManagedThreadId;
return string.Format("线程池线程 id 是: {0}", threadId);
}

回调函数的执行,是在EndInvoke之后执行的。
(二)异步方法BeginInvoke和EndInvoke的更多相关文章
- c#委托中的同步和异步方法即BeginInvoke和EndInvoke
学习多线程之前我们先了解一下电脑的一些概念,比如进程,线程,这个参考https://www.cnblogs.com/loverwangshan/p/10409755.html 这篇文章.今天我们接着来 ...
- C#线程系列讲座(1):BeginInvoke和EndInvoke方法
一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能,将要执行的任务分解成多个子任务执行.这就需要在同一个进程中开启多个 ...
- [转]BeginInvoke和EndInvoke方法浅析
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提 ...
- delegate 中的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 ...
- C#线程 BeginInvoke和EndInvoke使用方法
一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能,将要执行的任务分解成多个子任务执行.这就需要在同一个进程中开启多个 ...
- C#线程应用实例(part1) 之 BeginInvoke和EndInvoke
最近这个公司是做 winfrom 开发的 , 这段时间就好好的学学WCF , 公司框架什么的自己去琢磨! 这里主要写一些 winfrom 中 用到的一些陌生 技术 1.BeginInvoke 以前B ...
随机推荐
- IIS 发布 dedecms 网站教程
这里只是说明了配置 php 前后 iis 默认网站属性的变化,其实在配置完 php 后系统的环境变 量等也是发生了相应的变化了的, 这里就不一一列举了, 这些只有在你手动完成 php 的配置 之后才能 ...
- 三续ASM
在ASM的Core API中使用的是访问者模式来实现对类的操作,主要包含如下类: 一.ClassVisitor接口: 在这个接口中主要提供了和类结构同名的一些方法,这些方法可以对相应的类结构进行操作. ...
- Cannot complete the install because one or more required items could not be found
弄了一天的subclipse也没装上,郁闷~~~~~~~~ 无论采用本地安装还是站点安装都不行,在安装的时候显示错误: Cannot complete the install because one ...
- [转]js 正则表达式
一.正则表达式中包括的元素 1.原子(普通字符:a-z A-Z 0-9 .原子表. 转义字符) 2.元字符 (有特殊功能的字符) 3.模式修正符 (系统内置部分字符 i .m.S.U…) 二.正则表达 ...
- python3 第八章 - 完善九九乘法表
前面我们在第四章的时候挖了个坑:怎么用优雅的方式来打印九九乘法表.这一章我们就来填上这个坑. 首先,我们再来看下九九乘法表是什么样子的 1 x 1 = 1 1 x 2 = 2 2 x 2 = 4 1 ...
- POI--HSSFCellStyle类
通过POI来进行单元格格式的设定 设定格式使用「HSSFCellStyle」类.它有一个构造方法: protected HSSFCellStyle(short index, ExtendedForma ...
- linkin大话设计模式--简单工厂
linkin大话设计模式--工厂方法 什么是工厂方法:将多个类对象交给工厂来生成的设计被称为简单工厂模式,个人认为主要是为了实现解耦,在代码重构的时候会很重要. 代码如下: public class ...
- Ceph,TFS,FastDFS,MogileFS,MooseFS,GlusterFS 对比
系统整体对比 对比说明 /文件系统 TFS FastDFS MogileFS MooseFS GlusterFS Ceph 开发语言 C++ C Perl C C C++ 开源协议 GPL V2 GP ...
- Java并发系列[1]----AbstractQueuedSynchronizer源码分析之概要分析
学习Java并发编程不得不去了解一下java.util.concurrent这个包,这个包下面有许多我们经常用到的并发工具类,例如:ReentrantLock, CountDownLatch, Cyc ...
- sed&awk第二版读书笔记
1. POSIX标准对正则表达式字符和操作符的含义进行了形式化.这种标准定义了两类正则表达式:基本的正则表达式(BRE),grep和sed使用这种正则表达式;扩展的表达式,egrep和awk使用这种正 ...