Catching exceptions in task-factory.

This commit is contained in:
benbierens 2023-11-14 13:28:50 +01:00
parent 5996c0fa63
commit db55792f42
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 25 additions and 6 deletions

View File

@ -16,7 +16,14 @@ public class Program
Cancellation.Cts.Cancel(); Cancellation.Cts.Cancel();
}; };
try
{
runner.Run(); runner.Run();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Done."); Console.WriteLine("Done.");
} }
} }

View File

@ -59,7 +59,7 @@ namespace ContinuousTests
overviewLog.Error("Test infra failure: SingleTestRun failed with " + ex); overviewLog.Error("Test infra failure: SingleTestRun failed with " + ex);
Environment.Exit(-1); Environment.Exit(-1);
} }
}); }, nameof(SingleTestRun));
} }
private void RunTest(Action<bool> resultHandler) private void RunTest(Action<bool> resultHandler)

View File

@ -5,11 +5,23 @@
private readonly object taskLock = new(); private readonly object taskLock = new();
private readonly List<Task> activeTasks = new List<Task>(); private readonly List<Task> activeTasks = new List<Task>();
public void Run(Action action) public void Run(Action action, string name)
{ {
lock (taskLock) lock (taskLock)
{ {
activeTasks.Add(Task.Run(action).ContinueWith(CleanupTask, null)); activeTasks.Add(Task.Run(() => CatchException(action, name)).ContinueWith(CleanupTask, null));
}
}
private void CatchException(Action action, string name)
{
try
{
action();
}
catch (Exception ex)
{
Console.WriteLine($"Exception in task '{name}': " + ex);
} }
} }

View File

@ -69,7 +69,7 @@ namespace ContinuousTests
overviewLog.Error("Test infra failure: TestLoop failed with " + ex); overviewLog.Error("Test infra failure: TestLoop failed with " + ex);
Environment.Exit(-1); Environment.Exit(-1);
} }
}); }, nameof(TestLoop));
} }
private void StartTest() private void StartTest()