While loop with delay inside in custom activity in state machine workflow.
- Hello,
I am doing state machine workflow and I want create some custom activity which I could use in different places. But I need some loop in this activity which do sth ten times every 30 minutes.
I heve tried to derive from SequntialActivity and put to this While activity and in this loop CodeActivity and Delay activity. But of course I can't put such made activity to the state activity (to StateInitialiyation because it can't contain delay activity; to EventDriven because first activity must derive from IEventActivity - and only first). I want delay activity instead of for example sleep() in code activity because I want workflow to unload to db when it's waiting.
So what I should do in such situation? Maybe I should implement IEventActivity? But I am afraid of this because I really have no idea how I should write this to resolve problem.
Maye there is other way to put this activity to state?
Or maybe I should unload workflow and the load it by myself? But how do this properly?
I would be glad for any suggestions.
Thanks,
Krzysztof
Answers
I think you're caught between conflicting design requirements. On the one hand, you're trying to build a long running custom activity which loops performing a periodic action, and on the other hand, you're trying to build a single custom activity that is useful everywhere. But an event handler in a state machine is not intended to be long running - the whole idea is that each event and its processing should be a short, sharp single unit. In fact, a state machine should only ever process one event at a time.
So what to do? I think that the right answer here is to factor your solution out. What if you have one custom activity (A) that performs the action to be repeated just once. Then in a state machine, use Delay followed by A to achieve the looping effect. Then build a second custom activity (B) that contains a While which in turn contains Delay followed by A. Now, B is a long running activity that can be used in a sequential flow, say.
You may have better ideas on how to factor this out. However, I think the point is that the problem, as you state it, needs to be factored out to resolve the conflicting design requirements you describe.
All Replies
Hello,
In fact we want to have an activity similar to Delay activity. In our case we want to persist workflow for some time (e.g. 30sec). After that period our activity should check if can “unlock” the workflow. “Unlock” information will be in database (some kind of flag/semaphore). If semaphore is still closed then workflow should be persisted for next 30 sec.
Is it possible to inherit from Delay activity and implement such functionality?
You could create a custom delay activity which itself decides whether to remain 'executing' or to switch to 'closed'.
My article (incl. source-code) may help you with that: http://blogs.infosupport.com/porint/archive/2007/09/04/Configurable-Delay-activity-for-WF.aspx
Dave, is there a recommendation by MS on how to use DelayActivities within a StateMachine? In your example, you've provided a solution for refactoring out the solution, but in the end, the activity still cannot be used within a state machine because I can't support both HandleExternalEvent and delay.
What I want is the ability to make an external method call and, if the call fails, to try again in an hour or so. If there's a more appropriate way to solve this than a delay activity, could you explain?
Thanks!
Shawn

