WF4 中的活动代码条件 [英] While activity code condition in WF4

查看:26
本文介绍了WF4 中的活动代码条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行以下操作时,while 循环永远不会结束.我正在调用一个方法来获取 while 循环条件的值.请告诉我我做错了什么?

When the following is executed, the while loop never ends. I'm calling a method to get the value for the while loop condition here. Please tell me what I'm doing wrong?

    using System;
    using System.Linq;
    using System.Activities;
    using System.Activities.Statements;
    using System.IO;

   namespace BuildActivities
    {
   public sealed class CheckFile : CodeActivity
     {

    public InArgument<string> DirectoryName;

    protected override void Execute(CodeActivityContext context)
    {
        Activity workflow = new Sequence
        {
            Activities =
            {
                new While
            {

                Condition = GetValue() ,

                Body = new Sequence
                {
                    Activities = {
                        new WriteLine
                            {
                                Text = "Entered"
                            },
                        new WriteLine
                            {
                                Text = "Iterating"
                            },
                            new Delay
                            {
                Duration = System.TimeSpan.Parse("00:00:01")

                            }
                    }
                }

                //Duration = System.TimeSpan.Parse("00:00:01")
            },
            new WriteLine()
            {
                Text = "Exited"
            }
        }
        };
        try
        {
            WorkflowInvoker.Invoke(workflow, TimeSpan.FromSeconds(30));
        }
        catch (TimeoutException ex)
        {
            Console.WriteLine("The File still exist. Build Service has not picked up the file.");
        }
    }


    public bool GetValue()
    {
        bool matched = false;
        matched = File.Exists(@"\\vw189\release\buildservice\conshare.txt");
        return matched;
    }

}

}

当代码执行时,我认为它只是检查一次 while 条件.因为,我写了一些 writeline 来检查它是如何工作的.我看到循环永远不会结束.我通过在循环运行时删除文件夹中的文件来测试这一点.有一项服务应该每 5 秒选择一次文件.这是为了确定该服务是否已启动并正在运行.

when the code executes, i think it is only checking the while condition for one time. because, i have written some writeline to check how it works. and i see the loop never ends. I test this by deleting the file in the folder when the loop is running. There is a service which should pick the file every 5 sec. this is to determine whether that service is up and running or not.

推荐答案

同样,我不明白您在做什么,但在 CodeActivity 中调用工作流是错误的.我会尽量给你一些选择.

Again, I don't understand what you're doing but having a workflow call inside a CodeActivity is wrong. I'll try to give you some options.

选项 1:

有一个 CodeActivity 返回一个布尔值,指示文件是否存在是标准/正确的方法.然后您可以在您的工作流程中使用此活动:

Having a CodeActivity which returns a boolean indicating if a file exists or not is the standard/correct way. Then you can use this activity on your workflow:

public sealed class CheckFile : CodeActivity<bool>
{
    public InArgument<string> FilePath { get; set; }

    protected override bool Execute(CodeActivityContext context)
    {
        return File.Exists(FilePath.Get(context));
    }
}

选项 2:

与您的代码一起,您将通过 InvokeMethod:

Going along with your code, you would call File.Exists() through InvokeMethod:

var workflow = new Sequence
{
    Activities =
    {
        new While
        {
            Condition = new InvokeMethod<bool>
            {
                TargetType = typeof (File),
                MethodName = "Exists",
                Parameters = { new InArgument<string>("c:\\file.txt") }
            },
            Body = new WriteLine {Text = "File still exists..."}
        },
        new WriteLine {Text = "File deleted."}
    }
};

PS:您的 GetValue 仅在由 WorkflowInvoker 运行之前构建和评估工作流时调用一次.如果您希望它成为动态,请使用像我上面向您展示的 InvokeMethod 之类的活动.再次强调,不要仅仅因为在 CodeActivity 内部就尝试使用工作流.

PS: Your GetValue is called only once when workflow is build and evaluated just before being run by WorkflowInvoker. If you want it to be dynamic use activities like InvokeMethod like I showed you above. Again, don't try to use workflows just because, specially inside a CodeActivity.

这篇关于WF4 中的活动代码条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆