首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WF4中的While活动代码条件

WF4中的While活动代码条件
EN

Stack Overflow用户
提问于 2013-06-05 03:35:47
回答 1查看 1.4K关注 0票数 0

当执行以下命令时,while循环永远不会结束。在这里,我调用一个方法来获取while循环条件的值。请告诉我我哪里做错了?

代码语言:javascript
运行
复制
    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条件一次。因为,我已经写了一些文字来检查它是如何工作的。我发现这个循环永远不会结束。我通过在循环运行时删除文件夹中的文件来测试这一点。有一个服务应该每隔5秒挑选一次文件。这是为了确定该服务是否已启动并运行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-07 05:53:23

再说一次,我不明白你在做什么,但是在CodeActivity中进行工作流调用是错误的。我会试着给你一些选择。

选项1:

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

代码语言:javascript
运行
复制
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调用File.Exists()

代码语言:javascript
运行
复制
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中使用工作流。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16926041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档