Search code examples
c#parallel-processingworkflow-foundation-4parallel-extensions

Execute single workflow instance in parallel


Let's say I have a workflow, and I want to execute it many times in parallel:

System.Threading.Tasks.Parallel.For(
    0, 
    100, 
    i => WorkflowInvoker.Invoke(
        new Workflow1(),  
        new Dictionary<string, object> { { "Num", i } }));

I wonder, is it legal to execute it this way:

var w = new Workflow1();
System.Threading.Tasks.Parallel.For(
    0, 
    100, 
    i => WorkflowInvoker.Invoke(
        w, 
        new Dictionary<string, object> { { "Num", i } }));

Solution

  • Yes it is "legal" to execute a workflow definition that way. The workflow definition itself is thread safe as far as invocation goes. In fact, it is highly recommended you always cache the workflow definition for reuse in your apps. This is because the definition is just a template for the execution, the execution itself results in an entirely new state each time.