WF:检查工作流应用程序是否已从自定义活动中取消
如何从 NativeActivity 检查工作流应用程序的“取消”方法是否被调用?
我尝试使用上下文的“IsCancellationRequested”属性,但作用不大。
这是我的样本:
public class Program
{
static void Main(string[] args)
{
ManualResetEventSlim mre = new ManualResetEventSlim(false);
WorkflowApplication app = new WorkflowApplication(new Sequence() { Activities = {new tempActivity(), new tempActivity() } });
app.Completed += delegate(WorkflowApplicationCompletedEventArgs e)
{
mre.Set();
};
app.Run(TimeSpan.MaxValue);
Thread.Sleep(2000);
app.BeginCancel(null,null);
mre.Wait();
}
}
public class tempActivity : NativeActivity
{
protected override void Execute(NativeActivityContext context)
{
Console.WriteLine("Exec tempActivity");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
Console.Write(".");
if (context.IsCancellationRequested)
return;
}
}
}
谢谢!
How can i check from my NativeActivity if the 'Cancel' method of the workflow application was invoked?
I tried to use the 'IsCancellationRequested' property of the context but it doesn`t much.
Here is my sample:
public class Program
{
static void Main(string[] args)
{
ManualResetEventSlim mre = new ManualResetEventSlim(false);
WorkflowApplication app = new WorkflowApplication(new Sequence() { Activities = {new tempActivity(), new tempActivity() } });
app.Completed += delegate(WorkflowApplicationCompletedEventArgs e)
{
mre.Set();
};
app.Run(TimeSpan.MaxValue);
Thread.Sleep(2000);
app.BeginCancel(null,null);
mre.Wait();
}
}
public class tempActivity : NativeActivity
{
protected override void Execute(NativeActivityContext context)
{
Console.WriteLine("Exec tempActivity");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
Console.Write(".");
if (context.IsCancellationRequested)
return;
}
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
工作流程中的几乎所有内容都是异步调度和执行的。这包括取消,因此执行中的阻塞可确保取消请求永远不会被处理。
您需要像这样编写活动:
Pretty much everything in workflow is scheduled and executed asynchronously. This includes cancellation so blocking in the Executes makes sure the cancel request is never processed.
You need to write the activity something like this: