使用不同的持续时间更改DelayActivity的持续时间

时间:2010-07-06 15:43:11

标签: workflow-foundation workflow-foundation-4

我的自定义活动有一个延迟,需要在不同的时间设置不同的值。第一次设置时,可能是5秒。第二次,可能是10秒,依此类推。怎么可以这样做...延迟没有ActivityContext lambda,它将允许我获得工作流变量。

3 个答案:

答案 0 :(得分:1)

如果您的活动派生自NativeActivity,则在CacheMetadata期间添加一个私有变量作为ImplementationVariable,并在您进行活动循环时设置此变量。类似的东西:

public class RetryTest : NativeActivity
{
    private Variable<int> count = new Variable<int>("Count", 1);
    private Variable<TimeSpan> delayInterval = new Variable<TimeSpan>("DelayInterval", TimeSpan.FromSeconds(5));
    private Delay delay = new Delay();

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.AddImplementationVariable(count);
        metadata.AddImplementationVariable(delayInterval);

        delay.Duration = new InArgument<TimeSpan>(delayInterval);

        metadata.AddImplementationChild(delay);
    }

    protected override void Execute(NativeActivityContext context)
    {
        Console.WriteLine("{0} - Execute", DateTime.Now.ToLongTimeString());
        count.Set(context, count.Get(context) + 1);
        context.ScheduleActivity(delay, OnDelayComplete);
    }

    private void OnDelayComplete(NativeActivityContext context, ActivityInstance completedInstance)
    {
        Console.WriteLine("{0} - DelayComplete", DateTime.Now.ToLongTimeString());
        if (count.Get(context) < 3)
        {

            delayInterval.Set(context, TimeSpan.FromSeconds(delayInterval.Get(context).Seconds * count.Get(context)));
            context.ScheduleActivity(delay, OnDelayComplete);
        }
        count.Set(context, count.Get(context) + 1);
    }
}

这应输出

8:49:26 a.m. - Execute
8:49:31 a.m. - DelayComplete
8:49:41 a.m. - DelayComplete

答案 1 :(得分:0)

持续时间是TimeSpan表达式。只需输入一个能够产生正确时间的公式。像TimeSpan.FromSeconds(5 * loopCounter)

之类的东西

答案 2 :(得分:0)

好吧,像Maurice说的那样使用“TimeSpan.FromSeconds(5 * loopCounter)”,然后从工具箱中添加一个asign活动,并设置loopCounter = loopCounter + 5 ......