WF4 Beta2增加了AsyncCodeActivity,在编写自定义活动时,可以实现Begin/End的异步执行方式。当异步操作执行时,不影响工作流中的其他活动,
但是工作流不会被持久化。
下面是例子:
namespace CaryAsync
{
public sealed class FileWriter : AsyncCodeActivity
{
public FileWriter()
: base()
{
}
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
{
string tempFileName = Path.GetTempFileName();
Console.WriteLine("Writing to file: " + tempFileName);
FileStream file = File.Open(tempFileName, FileMode.Create);
context.UserState = file;
byte[] bytes = UnicodeEncoding.Unicode.GetBytes("123456789");
return file.BeginWrite(bytes, 0, bytes.Length, callback, state);
}
protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
{
FileStream file = (FileStream)context.UserState;
try
{
file.EndWrite(result);
file.Flush();
}
finally
{
file.Close();
}
}
}
}
本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/11/12/WF4Beta2-Async.html,如需转载请自行联系原作者