locked
WorkflowApplication changing thread apartment state from STA to MTA? RRS feed

  • Question

  • I have a WPF application in which I am rehosting the WF4 workflow designer.  I want my users to be able to edit a workflow, execute and trace it (using visual workflow tracking), then edit it further.  I am using a Workflow Application to run the workflow.

    However, when the workflow finishes executing, I find that the thread apartment state has become MTA, which no longer allows me to create a new workflowdesigner (to go back to editing).  Worse, I cannot return the thread to STA mode, as I get an InvalidOperationException stating "Failed to set the specified COM apartment state.".  What can I do?

    UPDATE:

    oops, looks like I figured out the problem.  Turns out I was trying to continue my program in the workflow's execution thread because I was using the workflow application's OnWorkflowCompleted event.  Is there a way to use this event and yet continue executing back on the UI thread afterwards?

    Friday, July 15, 2011 4:26 PM

Answers

  • Inside OnWorkflowCompleted, you can use a Dispatcher.BeginInvoke(), SynchronizationContext.Post(), or thread-synchronization primitive like AutoResetEvent.Set() in order to signal the completion event back to your UI thread.
    Tim
    • Proposed as answer by Tim Lovell-Smith Tuesday, July 19, 2011 5:19 AM
    • Marked as answer by Andrew_Zhu Thursday, August 4, 2011 1:29 AM
    Friday, July 15, 2011 8:58 PM