locked
Runing WF4 in an STA thread RRS feed

  • Question

  • In WF3.5 I used the ManualWorkflowSchedulerService to run a workflow on the STA thread I was already running on.

    Does anyone know how to do this in WF4 Beta 2.  I am currently using WorkflowApplication to run my workflow.

    Thanks
    Thursday, November 5, 2009 2:19 PM

Answers

  • WF 4 uses the SynchronizationContext abstraction to determine which thread to use for execution.  When using WorkflowApplication you can set the SynchronizationContext property to specify the context to use (DispatcherSynchronizationContext, WpfSynchronizationContext, AspNetSynchronizationContext, etc).  When the scheduler needs a thread to run workflow it will call Post on the supplied sync context to obtain a thread.

    As for WorkflowInvoker, you have three options:

    * Invoke will run the provided workflow from start to finish on the current thread.  The call blocks until the workflow completes or times out.
    * Begin/EndInvoke will run the provided workflow from start to finish on whichever thread happens to be available.  As an example, the workflow will probably run to its first blocking point on the thread which called BeginInvoke.  The next burst of work will run on whichever thread unblocked the workflow, and so on and so forth.  Again, the invocation does not complete until the workflow completes or times out.
    * InvokeAsync will run the provided workflow from start to finish on a thread obtained from SynchronizationContext.Current.  As with Begin/EndInvoke, the workflow might run in multiple pulses thereby calling SyncContext.Post multilpe times.  This is the Whidbey Async model and follows all of the rules set forth by that pattern.  InvokeCompleted will not be raised until the workflow completes or times out.

    Thursday, November 5, 2009 5:59 PM

All replies

  • If you use the WorkflowInvoker you get a similar experience as the ManualWorkflowSchedulerServiceas the workflow runs on your thread. It does limit you somewhat with what you can do though as it doesn't support tracking or persistence.
    Thursday, November 5, 2009 5:01 PM
  • WF 4 uses the SynchronizationContext abstraction to determine which thread to use for execution.  When using WorkflowApplication you can set the SynchronizationContext property to specify the context to use (DispatcherSynchronizationContext, WpfSynchronizationContext, AspNetSynchronizationContext, etc).  When the scheduler needs a thread to run workflow it will call Post on the supplied sync context to obtain a thread.

    As for WorkflowInvoker, you have three options:

    * Invoke will run the provided workflow from start to finish on the current thread.  The call blocks until the workflow completes or times out.
    * Begin/EndInvoke will run the provided workflow from start to finish on whichever thread happens to be available.  As an example, the workflow will probably run to its first blocking point on the thread which called BeginInvoke.  The next burst of work will run on whichever thread unblocked the workflow, and so on and so forth.  Again, the invocation does not complete until the workflow completes or times out.
    * InvokeAsync will run the provided workflow from start to finish on a thread obtained from SynchronizationContext.Current.  As with Begin/EndInvoke, the workflow might run in multiple pulses thereby calling SyncContext.Post multilpe times.  This is the Whidbey Async model and follows all of the rules set forth by that pattern.  InvokeCompleted will not be raised until the workflow completes or times out.

    Thursday, November 5, 2009 5:59 PM