简体   繁体   English

WF:检查工作流程应用程序是否已从自定义活动中取消

[英]WF: Check if the workflow application was cancelled from a custom activity

How can i check from my NativeActivity if the 'Cancel' method of the workflow application was invoked? 我如何从NativeActivity中检查工作流程应用程序的“取消”方法是否被调用?

I tried to use the 'IsCancellationRequested' property of the context but it doesn`t much. 我尝试使用上下文的“ IsCancellationRequested”属性,但是用处不大。

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! 谢谢!

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: 您需要这样编写活动:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM