Search code examples
c#multithreadingasp.net-mvc-3workflow-foundation-4

Properly completing execution of a Workflow in WF 4 and MVC3


I have a workflow that runs (hosted by WorkflowApplication), sends an email, then persists using a bookmark. The email has a link that allows the workflow to resume at that bookmark, which it does properly. However, after the workflow finishes, the webpage never loads.

It's like the page is waiting for the workflow to finish but it never does. I am very new to workflow and even newer to multithreading, so my question is:

Is there anything special I need to do to exit the workflow completely so control is given back to the main thread? Like a delegate method I'm missing?

Cheers


Solution

  • So I figured it out right after I posted, so I'm sorry. Here it is though:

    Because the Workflow was on a seperate thread, it was not syncing with the main thread once it was done. So I needed to add a method in the Completed delegate to make sure it synced up with the main thread after it was done. I just forgot that. Hope this helps someone.

    First, you need to set and auto reset event and a WorkflowApplication object

     private static AutoResetEvent _syncEvent = new AutoResetEvent(false);
     private static WorkflowApplcation _wfApp = new WorkflowApplication(new MyActivity());
    

    Then in the delegate methods of the wfApp, make sure to call:

     _syncEvent.Set();
    

    So:

    wfApp.Completed = (e) =>
                  {
                    Debug.WriteLine("Workflow completed: {0}", e.Outputs["Message"]);
                    _syncEvent.Set();
                  };
    

    This last part is what I was missing