From 148602d8d38bff9cd078f43bd14725e7564c2204 Mon Sep 17 00:00:00 2001 From: Michael Higgins Date: Sat, 10 Oct 2020 11:41:32 -0400 Subject: [PATCH] catch thread exceptions --- CenterTaskbar/Program.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/CenterTaskbar/Program.cs b/CenterTaskbar/Program.cs index 786f8b1..d5fa72d 100644 --- a/CenterTaskbar/Program.cs +++ b/CenterTaskbar/Program.cs @@ -168,11 +168,19 @@ private void CancelPositionThread() _loopCancellationTokenSource.Cancel(); Parallel.ForEach(_positionThreads.Values.ToList(), theTask => { - // Give the thread time to exit gracefully. - if (theTask.Wait(OneSecond * 3)) return; - - Debug.WriteLine("Timeout waiting for task to cancel!"); - theTask.Dispose(); + try + { + // Give the thread time to exit gracefully. + if (theTask.Wait(OneSecond * 3)) return; + } + catch (OperationCanceledException e) + { + Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}"); + } + finally + { + theTask.Dispose(); + } }); _loopCancellationTokenSource = new CancellationTokenSource();