Ewild

Hey All,
I'm trying to persist a workflow when my application closes unexpectedly. I have the SqlWorkflowPersistenceService running just fine. The frmMain_FormClosing works if the workflow is running, but fails if it has already completed.
What should I be testing to see if the workFlowInstance is competed, or still running

Eric



AutoResetEvent m_WaitHandle;
WorkflowInstance m_WorkflowInstance;


private void btnStartWorkFlow_Click(object sender, EventArgs e)
{
Thread tl = new Thread(StartWorkFlow);
tl.Start();
}

private void StartWorkFlow()
{
using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
m_WaitHandle = new AutoResetEvent(false);

workflowRuntime.AddService(new SqlWorkflowPersistenceService(Properties.Resources.SqlPersistenceConnectionString));

workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
workflowRuntime.WorkflowTerminated += new EventHandler<WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);
workflowRuntime.WorkflowIdled += new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);
workflowRuntime.WorkflowPersisted += new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowPersisted);
workflowRuntime.WorkflowUnloaded += new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowUnloaded);
workflowRuntime.WorkflowLoaded += new EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowLoaded);

////creates parameters for the workflow which then get passed to the various activities.
Dictionary<string, object> parameters = new Dictionary<string, object>();

m_WorkflowInstance = workflowRuntime.CreateWorkflow(typeof(EWild.Workflows.WorkFlowLibrary.OptionImportWorkflow), parameters);

m_WorkflowInstance.Start();

System.Diagnostics.Debug.WriteLine("Starting WorkFlow");

m_WaitHandle.WaitOne();
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (m_WorkflowInstance != null)
{
System.Diagnostics.Debug.WriteLine("Trying to unload WorkFlow.");
UnloadWorkFlow();
}
}
private void UnloadWorkFlow()
{
try
{
m_WorkflowInstance.Unload();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());

}
}


Re: Windows Workflow Foundation Determine if WorkFlow is loaded.

Vignesh Kannappan - MSFT

Hi Ewild - i see you are using m_WorkflowInstance property before unloading a workflow. Is it being properly reset to "null" in the completed/terminated event handlers I see your subscription code, but not the actual code handlers. So I think you are running into the issue because after completeion its not being reset properly and you are trying to unload it which is an invalid tranisition.

Thanks,

Vignesh






Re: Windows Workflow Foundation Determine if WorkFlow is loaded.

Angel Azcarraga - MSFT

It looks like you subscribe to all the instance events (completed, terminated, etc.), so you should know if something happened when these are called. If you keep a bool around that you set when the workflow completed event is raised, you could test this in your _FormClosing method before you try to unload.

Another thing to keep in mind is that when the workflow runtime is shutdown (goes out of scope), all running instances are automatically unloaded for you.

-Angel






Re: Windows Workflow Foundation Determine if WorkFlow is loaded.

kushals

This is because you are trying to UnLoad a WF which i s already completed. For your question as to what can be done to know whether is instance is running - TheWorkflowCompletedEvent should tell you if the instance is done or not. you can have a flag set there. And then Check this flag within the frmMain_FormClosing method so that you dont UNload something that is already done.

Hope this helps!

Thanks,

Kushal.






Re: Windows Workflow Foundation Determine if WorkFlow is loaded.

Ewild

OK,
I guess the reason I did not see this as being the answer was that I am using the persistence service. There are two things happening here.
1) I am completing the worklow.
2) I am removing it from the persistence database.

I haven't tried this yet, but which happens first

workflowRuntime_WorkflowPersisted
workflowRuntime_WorkflowCompleted

If the Persisted event is fired, presumably the record in the persistence db should be deleted, and I won't try to restart this instance in future restarts. If the workflowRuntime_WorkflowPersisted fails, will the Completed fire How do I know both worked
Since the SqlPersistenceService tacks onto the workflow, how do I know what, or when I need to be involved for things like concurrency
Everything else made sence... I need to be more in charge, and put more code into the events to clean up code. I am trying to get through a book on WF and it looks like exceptions are an exceptional thing (as usual).

Thanks
Eric





Re: Windows Workflow Foundation Determine if WorkFlow is loaded.

kushals

As a general rule the Persisted event should fire first and if things go one fine WorkflowCompleted event should fire.

Well - How do I know both worked - I think U need to use Tracking to get more control/know as to what happened to the instance. tracking should give you a clear snapshot as to what went on during the life time of the WF instance. Some samples can be found here:

http://msdn2.microsoft.com/en-us/library/ms741724.aspx

Regarding your question as to if the Workflow persistence fails - most probably it should throw out an exception saying why persistence didnt take place. And once you get the exception no question of WF being completed.

Generally speaking the SQLPersistenceService should take care of everything and you shouldnt need to worry about Concurrency and stuff.

Hope this helps!

Thanks,

Kushal.