From 57876441fc34b92279e5ff57095b24de8d177262 Mon Sep 17 00:00:00 2001 From: Michael Higgins Date: Tue, 24 Nov 2020 15:43:10 -0500 Subject: [PATCH] better exception handling --- CenterTaskbar/Program.cs | 43 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/CenterTaskbar/Program.cs b/CenterTaskbar/Program.cs index 62798b5..ae2acf7 100644 --- a/CenterTaskbar/Program.cs +++ b/CenterTaskbar/Program.cs @@ -166,25 +166,34 @@ private void Exit(object sender, EventArgs e) private void CancelPositionThread() { - _loopCancellationTokenSource.Cancel(); - Parallel.ForEach(_positionThreads.Values.ToList(), theTask => + try { - 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 + _loopCancellationTokenSource.Cancel(); + Parallel.ForEach(_positionThreads.Values.ToList(), theTask => { - theTask.Dispose(); - } - }); - - _loopCancellationTokenSource = new CancellationTokenSource(); + 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(); + } + }); + } + catch (OperationCanceledException e) + { + Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}"); + } + finally + { + _loopCancellationTokenSource = new CancellationTokenSource(); + } } private void Restart(object sender, EventArgs e)