danielgerlag / workflow-core

Lightweight workflow engine for .NET Standard
MIT License
5.4k stars 1.2k forks source link

Multiple processes are multi-threaded in parallel? #1242

Closed hypnosis01 closed 2 weeks ago

hypnosis01 commented 9 months ago

public class AspSample : StepBody { private static readonly Logger log = LogManager.GetCurrentClassLogger(); private static volatile int _counter = 0;

public AspSample()
{
    log.Debug("AspSample init ...");
}

public override ExecutionResult Run(IStepExecutionContext context)
{
    if (_counter % 2 == 0) 
    {
        Thread.Sleep(2000);
    }
    log.Debug($"StepBody: {nameof(AspSample)}");
    AspSample._counter++;
    return ExecutionResult.Next();
}

}

var host = serviceProvider.GetService(); host.StartWorkflow("PTWorkFlow", 1, null); host.StartWorkflow("PTWorkFlow", 1, null); host.StartWorkflow("PTWorkFlow", 1, null); host.StartWorkflow("PTWorkFlow", 1, null);

Multiple process implementations were initiated。

image

Multiple processes are executed serially.......

hypnosis01 commented 9 months ago

How do I make each process instance execute in a separate thread?

Blackclaws commented 2 months ago

Don't use Thread.Sleep, use Async and Task.Delay and it will work as expected. The problem is Thread.Sleep blocks the thread that enters the workflow execution preventing additional executions from starting.

That being said I think this behavior should not need this to work.