![](/img/trans.png)
[英]Windows WF 3.5 How to drop activity on Custom Composite Activity in Workflow
[英]WF: Check if the workflow application was cancelled from a custom activity
我如何從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;
}
}
}
謝謝!
工作流中的幾乎所有內容都是異步安排和執行的。 這包括取消,因此“執行”中的阻塞可確保從未處理過取消請求。
您需要這樣編寫活動:
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.