My custom activity has a delay that will need to be set 开发者_JAVA技巧different values at different times. The first time it is set, it might be 5 seconds. The second time, it might be 10 seconds, and so on. How can one do this... Delay does not have an ActivityContext lambda that will allow me to get a workflow variable.
If your activity derives from NativeActivity then add a private Variable as an ImplementationVariable during CacheMetadata and set this as you progress through your activity loops. Something like:
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);
}
}
This should output
8:49:26 a.m. - Execute
8:49:31 a.m. - DelayComplete
8:49:41 a.m. - DelayComplete
The Duration is a TimeSpan expression. Just enter a formula that results in the correct amount of time. Something like TimeSpan.FromSeconds(5 * loopCounter)
Well, use "TimeSpan.FromSeconds(5 * loopCounter)" like Maurice said, and then add an asign activity from tool box, and set loopCounter = loopCounter + 5 ...
精彩评论