.NET 异步处理(转载自 http://www.cnblogs.com/czy/archive/2010/01/14/1648139.html)
来源:互联网 发布:ncbi批量下载sra数据 编辑:程序博客网 时间:2024/05/16 01:06
这几天,看WF本质论,里面提到了.net的异步处理。由于里面使用的是代码片段,所以有点看不懂。于是下定决心,温习一下.net中的异步处理。
使用C#在.net开发已经有5年了,最初使用.net中的异步处理大约是在4年前。当时,只是为了实现要求的功能,没有详细研究。这也难怪看WF时会头晕(基础不牢的后果呀)。
首先,我们分析一下异步处理的环境
- 需要在当前线程中获取返回值
- 不需要在当前线程中获取返回值,但是仍然需要对返回值做处理
对于第1中情况,还可以继续细分
- 在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前线程中获取T的返回值
- 在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值
- 在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值
下面,我将一一给出例子:
1.1 在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前线程中获取T的返回值
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Windows.Forms;
using
System.Threading;
using
System.Runtime.Remoting.Messaging;
namespace
FirstWF
{
static
class
Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static
void
Main()
{
AsyncFuncDelegate caller =
new
AsyncFuncDelegate(Func);
Console.WriteLine(
"Input number please..."
);
IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()),
null
,
null
);
Console.WriteLine(
"Implement other tasks"
);
Thread.Sleep(7000);
Console.WriteLine(
"Implement other tasks end ..."
);
Console.WriteLine(
"Get user's input"
);
Console.WriteLine(caller.EndInvoke(result));
Console.ReadLine();
}
delegate
string
AsyncFuncDelegate(
int
userInput);
static
string
Func(
int
userInput)
{
Console.WriteLine(
"Func start to run"
);
Console.WriteLine(
"..."
);
Thread.Sleep(5000);
Console.WriteLine(
"Func end to run"
);
return
userInput.ToString();
}
}
}
输出结果如下:
Implement other tasks
Func start to run
...
Func end to run
Implement other tasks end ...
Get user's input
56
1.2 在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值
static
void
Main()
{
AsyncFuncDelegate caller =
new
AsyncFuncDelegate(Func);
Console.WriteLine(
"Input number please..."
);
IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()),
null
,
null
);
Console.WriteLine(
"Implement task 1"
);
result.AsyncWaitHandle.WaitOne();
result.AsyncWaitHandle.Close();
Console.WriteLine(
"Implment task 2"
);
Console.WriteLine(
"Get user's input"
);
Console.WriteLine(caller.EndInvoke(result));
Console.ReadLine();
}
输出结果如下:
Input number please...
25
Implement task 1
Func start to run
...
Func end to run
Implment task 2
Get user's input
25
1.3 在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值
[STAThread]
static
void
Main()
{
AsyncFuncDelegate caller =
new
AsyncFuncDelegate(Func);
Console.WriteLine(
"Input number please..."
);
IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()),
null
,
null
);
while
(!result.IsCompleted)
{
Thread.Sleep(1000);
Console.Write(
">"
);
}
Console.WriteLine(
""
);
Console.WriteLine(
"Implement other task2"
);
Console.WriteLine(
"Get user's input"
);
Console.WriteLine(caller.EndInvoke(result));
Console.ReadLine();
}
输出结果如下:
Func start to run
...
>>>>>Func end to run
>
Implement other task2
Get user's input
23
2 不需要在当前线程中获取返回值,但是仍然需要对返回值做处理
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Windows.Forms;
using
System.Threading;
using
System.Runtime.Remoting.Messaging;
namespace
FirstWF
{
static
class
Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static
void
Main()
{
AsyncFuncDelegate caller =
new
AsyncFuncDelegate(Func);
Console.WriteLine(
"Input number please..."
);
caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()),
new
AsyncCallback(CallBackFunc),
"Message from Main thread."
);
Console.WriteLine(
"Main thread ends"
);
Console.ReadLine();
}
delegate
string
AsyncFuncDelegate(
int
userInput);
static
string
Func(
int
userInput)
{
Console.WriteLine(
"Func start to run"
);
Console.WriteLine(
"..."
);
Thread.Sleep(5000);
Console.WriteLine(
"Func end to run"
);
return
userInput.ToString();
}
static
void
CallBackFunc(IAsyncResult ar)
{
AsyncResult result = ar
as
AsyncResult;
string
inputMessage = result.AsyncState
as
string
;
AsyncFuncDelegate caller = result.AsyncDelegate
as
AsyncFuncDelegate;
Console.WriteLine(
"call back starts"
);
Console.WriteLine(inputMessage);
Console.WriteLine(
"The input number is : "
+ caller.EndInvoke(ar));
Console.WriteLine(
"call back ends"
);
}
}
}
输出结果如下:
Input number please...
23
Main thread ends
Func start to run
...
Func end to run
call back starts
Message from Main thread.
The input number is : 23
call back ends
记得以前的代码,写的都不是很好。虽然call.BeginInvoke可以开始异步调用,但几乎就没有使用过EndInvoke。EndInvoke可以保证异步调用被正常结束,使代码更加健康。
异步调用,可以使代码具有更高的执行效率,但是在异步调用时,应该有一个健康的使用习惯。
http://www.cnblogs.com/czy/archive/2010/01/14/1648139.html- .NET 异步处理(转载自 http://www.cnblogs.com/czy/archive/2010/01/14/1648139.html)
- .Net Remoting (转载)(转自http://www.cnblogs.com/JimmyZhang/archive/2008/07/26/1252183.html)
- .net常用的类库网络转载(http://www.cnblogs.com/and/archive/2010/03/24/1693970.html)
- ListView应用(转自http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html)
- 超链接按钮(转自http://www.cnblogs.com/cxy521/archive/2008/01/22/1048621.html)
- Matlab 文件操作(转载http://www.cnblogs.com/longdouhzt/archive/2013/01/29/2881749.html)
- 正则表达式备忘-----------转载自http://www.cnblogs.com/KissKnife/archive/2008/03/23/1118423.html
- 单点登录(转载自:http://www.cnblogs.com/shanyou/archive/2009/08/30/1556659.html)
- js call()用法 转载自--http://www.cnblogs.com/sweting/archive/2009/12/21/1629204.html
- 数据库ID---转载自http://www.cnblogs.com/kissdodog/archive/2013/05/28/3102862.html
- 转载自:http://www.cnblogs.com/chenyadong/archive/2011/10/25/2223610.html
- MFC的资源切换AFX_MANAGE_STATE (转载http://www.cnblogs.com/ntrgx/archive/2010/11/14/1877199.html)
- 结构体对齐详解(转载自http://www.cnblogs.com/motadou/archive/2009/01/17/1558438.html)
- IOS 字符串处理 转载来自:http://www.cnblogs.com/martin1009/archive/2012/06/07/2540123.html
- Linux C语言错误处理(转载)http://www.cnblogs.com/nliao/archive/2010/09/09/1822647.html
- Qt实现自定义按钮的三态效果(转载自http://www.cnblogs.com/emouse/archive/2013/07/14/3189319.html)
- VS2010版本介绍(转自:http://www.cnblogs.com/Leo_wl/archive/2010/06/02/1750035.html)
- C++强大背后(转自http://www.cnblogs.com/miloyip/archive/2010/09/17/behind_cplusplus.html)
- 法国INSIDE Secure 公司出品的NFC控制芯片
- 2012年的新年第一个任务——工作环境的冒烟测试
- Android状态栏通知Notification使用小结
- arcgis数据格式 raster to polygon出现不是整型的错误
- DataNode节点上数据块接收器——BlockReceiver
- .NET 异步处理(转载自 http://www.cnblogs.com/czy/archive/2010/01/14/1648139.html)
- 不能打开数据库的一次处理 ORA-01092: ORACLE instance terminated. Disconnection forced
- tomcat远程调试配置方法
- 运行时库概念
- 浅析 Java Thread.join()
- live555 vlc sourcecode
- C# String和StringBuilder简单对比【整理】
- 在windows的PHP环境下安装Nginx。
- linux发行版i386/i686/x86-64/的区别