Pause and Resume a workflow
Workflows pause and resume in response to bookmarks and blocking activities such as Delay, but a workflow can also be explicitly paused, unloaded, and resumed by using persistence.
Pause a workflow
To pause a workflow, use Unload. This method requests that the workflow persist and unload, and will throw a TimeoutException if the workflow does not unload in 30 seconds.
try
{
// attempt to unload will fail if the workflow is idle within a NoPersistZone
application.Unload(TimeSpan.FromSeconds(5));
}
catch (TimeoutException e)
{
Console.WriteLine(e.Message);
}
Resume a workflow
To resume a previously paused and unloaded workflow, use Load. This method loads a workflow from a persistence store into memory.
WorkflowApplication application = new WorkflowApplication(activity);
application.InstanceStore = instanceStore;
application.Load(id);
Example
The following code sample demonstrates how to pause and resume a workflow by using persistence.
static string bkName = "bkName";
static void Main(string[] args)
{
StartAndUnloadInstance();
}
static void StartAndUnloadInstance()
{
AutoResetEvent waitHandler = new AutoResetEvent(false);
WorkflowApplication wfApp = new WorkflowApplication(GetDelayedWF());
SqlWorkflowInstanceStore instanceStore = SetupSqlpersistenceStore();
wfApp.InstanceStore = instanceStore;
wfApp.Extensions.Add(SetupMyFileTrackingParticipant);
// Persists application state and removes it from memory
wfApp.PersistableIdle = (e) => {
return PersistableIdleAction.Unload;
};
wfApp.Unloaded = (e) => {
waitHandler.Set();
};
Guid id = wfApp.Id;
wfApp.Run();
waitHandler.WaitOne();
LoadAndCompleteInstance(id);
}
static void LoadAndCompleteInstance(Guid id)
{
Console.WriteLine("Press <enter> to load the persisted workflow");
Console.ReadLine();
AutoResetEvent waitHandler = new AutoResetEvent(false);
WorkflowApplication wfApp = new WorkflowApplication(GetDelayedWF());
wfApp.InstanceStore =
new SqlWorkflowInstanceStore(ConfigurationManager.AppSettings["SqlWF4PersistenceConnectionString"].ToString());
wfApp.Completed = (workflowApplicationCompletedEventArgs) => {
Console.WriteLine("\nWorkflowApplication has Completed in the {0} state.",
workflowApplicationCompletedEventArgs.CompletionState);
};
wfApp.Unloaded = (workflowApplicationEventArgs) => {
Console.WriteLine("WorkflowApplication has Unloaded\n");
waitHandler.Set();
};
wfApp.Load(id);
wfApp.Run();
waitHandler.WaitOne();
}
public static Activity GetDelayedWF()
{
return new Sequence {
Activities ={
new WriteLine{Text="Workflow Started"},
new Delay{Duration=TimeSpan.FromSeconds(10)},
new WriteLine{Text="Workflow Ended"}
}
};
}
private static SqlWorkflowInstanceStore SetupSqlpersistenceStore()
{
string connectionString = ConfigurationManager.AppSettings["SqlWF4PersistenceConnectionString"].ToString();
SqlWorkflowInstanceStore sqlWFInstanceStore = new SqlWorkflowInstanceStore(connectionString);
sqlWFInstanceStore.InstanceCompletionAction = InstanceCompletionAction.DeleteAll;
InstanceHandle handle = sqlWFInstanceStore.CreateInstanceHandle();
InstanceView view = sqlWFInstanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(5));
handle.Free();
sqlWFInstanceStore.DefaultInstanceOwner = view.InstanceOwner;
return sqlWFInstanceStore;
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.