Skip to content

Commit

Permalink
Improved Engine.Start
Browse files Browse the repository at this point in the history
  • Loading branch information
kbilsted committed Nov 1, 2023
1 parent a072bb2 commit 24507c7
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/Product/GreenFeetWorkFlow/WorkflowEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,24 @@ public void Start(
string? engineName = null,
CancellationToken? stoppingToken = null)
{
static bool RunIt(Worker x, CancellationToken? stoppingToken)
{
x.StartAsync(stoppingToken ?? CancellationToken.None)
.GetAwaiter()
.GetResult();
return true;
}

Init(configuration, engineName);

Threads = Workers!
.Select((x, i) =>
{
var t = new Thread(async () => await x.StartAsync(stoppingToken ?? CancellationToken.None))
var t = new Thread(() => RunIt(x, stoppingToken))
{
Name = x.WorkerName,
IsBackground = true // c# automatically shuts down background threads when all foreground threads are terminated
};

return t;
})
.ToArray();
Expand All @@ -84,7 +91,20 @@ public void Start(
thread.Start();

foreach (var thread in Threads)
thread.Join();
{
try
{
var threadDied = thread.Join(-1);

if (threadDied)
logger.LogError($"Thread '{thread.Name}' has died! An exception was thrown and either exception not caught or async call not awaited", null, null);
}
catch (Exception e)
{
logger.LogError($"Exception during thread execution on thread '{thread.Name}'", e, null);
throw;
}
}
}

/// <summary> Starts the engine using 1 or more background threads in an async context</summary>
Expand All @@ -94,9 +114,9 @@ public async Task StartAsync(
CancellationToken? stoppingToken = null)
{
Init(configuration, engineName);

var token = stoppingToken ?? CancellationToken.None;

var tasks = Workers!.Select(w => w.StartAsync(token));
await Task.WhenAll(tasks.ToArray());
}
Expand Down

0 comments on commit 24507c7

Please sign in to comment.