From 9d7e2b9003b97c70e95e99eb77d14c0514f44d96 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 12 Jun 2023 01:33:52 +0800 Subject: [PATCH] update multi-exception in threads --- src/ThreadRunner.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ThreadRunner.cs b/src/ThreadRunner.cs index f9eb2d9..62048d4 100644 --- a/src/ThreadRunner.cs +++ b/src/ThreadRunner.cs @@ -11,10 +11,12 @@ namespace DCFApixels.DragonECS private static ThreadWorkerHandler _worker; private static ThreadWorkerHandler _nullWorker = delegate { }; private static int[] _entities = new int[64]; + private static List _catchedExceptions; private static void ThreadProc(object obj) { - ref ThreadReacord record = ref _threads[(int)obj]; + int i = (int)obj; + ref ThreadReacord record = ref _threads[i]; while (Thread.CurrentThread.IsAlive) { @@ -25,10 +27,12 @@ namespace DCFApixels.DragonECS _worker.Invoke(new ReadOnlySpan(_entities, record.start, record.size)); record.doneWork.Set(); } - catch (Exception) + catch (Exception e) { + if (_catchedExceptions == null) + _catchedExceptions = new List(); + _catchedExceptions.Add(e); record.doneWork.Set(); - throw; } } } @@ -93,6 +97,12 @@ namespace DCFApixels.DragonECS } _worker = _nullWorker; + if(_catchedExceptions != null) + { + var exceptions = _catchedExceptions; + _catchedExceptions = null; + throw new AggregateException("Mutiplie exceptions", exceptions); + } } private struct ThreadReacord @@ -100,7 +110,6 @@ namespace DCFApixels.DragonECS public Thread thread; public ManualResetEvent runWork; public ManualResetEvent doneWork; - public int start; public int size; }