如果调用工作流应用程序的“取消”方法,我如何从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;
}
}
}
谢谢!
答案 0 :(得分:2)
工作流中的所有内容都是异步调度和执行的。这包括取消,因此在Executes中阻止,确保永远不会处理取消请求。
你需要写下这样的活动:
public class tempActivity : NativeActivity
{
private Activity Delay { get; set; }
private Variable<int> Counter { get; set; }
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
Counter = new Variable<int>();
Delay = new Delay() { Duration = TimeSpan.FromSeconds(1) };
metadata.AddImplementationChild(Delay);
metadata.AddImplementationVariable(Counter);
base.CacheMetadata(metadata);
}
protected override void Execute(NativeActivityContext context)
{
OnCompleted(context, null);
}
private void OnCompleted(NativeActivityContext context, ActivityInstance completedInstance)
{
var counter = Counter.Get(context);
if (counter < 10 && !context.IsCancellationRequested)
{
Console.Write(".");
Counter.Set(context, counter + 1);
context.ScheduleActivity(Delay, OnCompleted);
}
}
}