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

发布于 2024-09-25 08:28:11 字数 1040 浏览 6 评论 0原文

如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2024-10-02 08:28:11

工作流程中的几乎所有内容都是异步调度和执行的。这包括取消,因此执行中的阻塞可确保取消请求永远不会被处理。

您需要像这样编写活动:

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);
        }
    }
}

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文