当执行以下命令时,while循环永远不会结束。在这里,我调用一个方法来获取while循环条件的值。请告诉我我哪里做错了?
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秒挑选一次文件。这是为了确定该服务是否已启动并运行。
发布于 2013-06-07 05:53:23
再说一次,我不明白你在做什么,但是在CodeActivity中进行工作流调用是错误的。我会试着给你一些选择。
选项1:
让CodeActivity返回指示文件是否存在的布尔值是标准/正确的方式。然后,您可以在工作流程中使用此活动:
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()
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中使用工作流。
https://stackoverflow.com/questions/16926041
复制相似问题