From 856a1a057c06433655dc06708306b21ba9cc907d Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 12 Feb 2024 02:50:51 +0300 Subject: [PATCH] Code refactoring --- .../Cryptographic/HashChecker.cs | 32 ++++++------ ShareX.HelpersLib/SingleInstanceManager.cs | 17 +++--- ShareX.HelpersLib/TaskEx.cs | 26 +++++----- .../Forms/RegionCaptureForm.cs | 12 +++-- .../Helpers/WindowsRectangleList.cs | 52 +++++++++---------- .../FileUploaders/SendSpace.cs | 6 +-- 6 files changed, 73 insertions(+), 72 deletions(-) diff --git a/ShareX.HelpersLib/Cryptographic/HashChecker.cs b/ShareX.HelpersLib/Cryptographic/HashChecker.cs index 84d5676da..4efb6b32b 100644 --- a/ShareX.HelpersLib/Cryptographic/HashChecker.cs +++ b/ShareX.HelpersLib/Cryptographic/HashChecker.cs @@ -55,19 +55,22 @@ public async Task Start(string filePath, HashType hashType) IsWorking = true; Progress progress = new Progress(OnProgressChanged); - cts = new CancellationTokenSource(); - result = await Task.Run(() => - { - try - { - return HashCheckThread(filePath, hashType, progress, cts.Token); - } - catch (OperationCanceledException) - { - } - return null; - }, cts.Token); + using (cts = new CancellationTokenSource()) + { + result = await Task.Run(() => + { + try + { + return HashCheckThread(filePath, hashType, progress, cts.Token); + } + catch (OperationCanceledException) + { + } + + return null; + }, cts.Token); + } IsWorking = false; } @@ -77,10 +80,7 @@ public async Task Start(string filePath, HashType hashType) public void Stop() { - if (cts != null) - { - cts.Cancel(); - } + cts?.Cancel(); } private string HashCheckThread(string filePath, HashType hashType, IProgress progress, CancellationToken ct) diff --git a/ShareX.HelpersLib/SingleInstanceManager.cs b/ShareX.HelpersLib/SingleInstanceManager.cs index d25c4aa7c..8fe774c05 100644 --- a/ShareX.HelpersLib/SingleInstanceManager.cs +++ b/ShareX.HelpersLib/SingleInstanceManager.cs @@ -44,7 +44,7 @@ public class SingleInstanceManager : IDisposable private static readonly string PipeName = $"{Environment.MachineName}-{Environment.UserName}-{AppName}"; private readonly Mutex mutex; - private CancellationTokenSource cancellationSource; + private CancellationTokenSource cts; public SingleInstanceManager(bool isSingleInstance, string[] args) { @@ -60,7 +60,9 @@ public SingleInstanceManager(bool isSingleInstance, string[] args) if (IsFirstInstance) { - Task.Run(() => ListenForConnectionsAsync()); + cts = new CancellationTokenSource(); + + Task.Run(() => ListenForConnectionsAsync(), cts.Token); } else { @@ -89,15 +91,13 @@ protected virtual void OnArgumentsReceived(string[] arguments) private async Task ListenForConnectionsAsync() { - cancellationSource = new CancellationTokenSource(); - - while (!cancellationSource.IsCancellationRequested) + while (!cts.IsCancellationRequested) { try { using (NamedPipeServerStream serverPipe = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous)) { - await serverPipe.WaitForConnectionAsync(cancellationSource.Token); + await serverPipe.WaitForConnectionAsync(cts.Token); using (BinaryReader reader = new BinaryReader(serverPipe, Encoding.UTF8)) { @@ -114,7 +114,7 @@ private async Task ListenForConnectionsAsync() } } } - catch (Exception e) when (cancellationSource.IsCancellationRequested) + catch when (cts.IsCancellationRequested) { } catch (Exception e) @@ -144,7 +144,8 @@ private void RedirectArgumentsToFirstInstance(string[] args) public void Dispose() { - cancellationSource?.Cancel(); + cts?.Cancel(); + cts?.Dispose(); mutex?.ReleaseMutex(); } } diff --git a/ShareX.HelpersLib/TaskEx.cs b/ShareX.HelpersLib/TaskEx.cs index 736a67aba..5848b1ef1 100644 --- a/ShareX.HelpersLib/TaskEx.cs +++ b/ShareX.HelpersLib/TaskEx.cs @@ -51,21 +51,21 @@ public async Task Run(Action action) IsCanceled = false; p = new Progress(OnProgressChanged); - cts = new CancellationTokenSource(); - try + using (cts = new CancellationTokenSource()) { - await Task.Run(action, cts.Token); - } - catch (OperationCanceledException) - { - IsCanceled = true; - } - finally - { - cts.Dispose(); - - IsRunning = false; + try + { + await Task.Run(action, cts.Token); + } + catch (OperationCanceledException) + { + IsCanceled = true; + } + finally + { + IsRunning = false; + } } } diff --git a/ShareX.ScreenCaptureLib/Forms/RegionCaptureForm.cs b/ShareX.ScreenCaptureLib/Forms/RegionCaptureForm.cs index 300123d23..000d75649 100644 --- a/ShareX.ScreenCaptureLib/Forms/RegionCaptureForm.cs +++ b/ShareX.ScreenCaptureLib/Forms/RegionCaptureForm.cs @@ -335,10 +335,14 @@ private void Prepare(Bitmap canvas = null) Task.Run(() => { - WindowsRectangleList wla = new WindowsRectangleList(); - wla.IgnoreHandle = handle; - wla.IncludeChildWindows = ShapeManager.IncludeControls; - ShapeManager.Windows = wla.GetWindowInfoListAsync(5000); + WindowsRectangleList wla = new WindowsRectangleList() + { + IgnoreHandle = handle, + IncludeChildWindows = ShapeManager.IncludeControls, + Timeout = 5000 + }; + + ShapeManager.Windows = wla.GetWindowInfoList(); }); } } diff --git a/ShareX.ScreenCaptureLib/Helpers/WindowsRectangleList.cs b/ShareX.ScreenCaptureLib/Helpers/WindowsRectangleList.cs index 85ac4f7c0..a434f6f27 100644 --- a/ShareX.ScreenCaptureLib/Helpers/WindowsRectangleList.cs +++ b/ShareX.ScreenCaptureLib/Helpers/WindowsRectangleList.cs @@ -35,42 +35,35 @@ public class WindowsRectangleList { public IntPtr IgnoreHandle { get; set; } public bool IncludeChildWindows { get; set; } + public int Timeout { get; set; } private List windows; private HashSet parentHandles; - - public List GetWindowInfoListAsync(int timeout) - { - List windowInfoList = null; - - Thread t = new Thread(() => - { - try - { - windowInfoList = GetWindowInfoList(); - } - catch - { - } - }); - - t.Start(); - - if (!t.Join(timeout)) - { - t.Abort(); - } - - return windowInfoList; - } + private CancellationTokenSource cts; public List GetWindowInfoList() { windows = new List(); parentHandles = new HashSet(); - EnumWindowsProc ewp = EvalWindow; - NativeMethods.EnumWindows(ewp, IntPtr.Zero); + try + { + if (Timeout > 0) + { + cts = new CancellationTokenSource(); + cts.CancelAfter(Timeout); + } + + EnumWindowsProc ewp = EvalWindow; + NativeMethods.EnumWindows(ewp, IntPtr.Zero); + } + catch + { + } + finally + { + cts?.Dispose(); + } List result = new List(); @@ -111,6 +104,11 @@ private bool EvalControl(IntPtr hWnd, IntPtr lParam) private bool CheckHandle(IntPtr handle, bool isWindow) { + if (cts != null && cts.IsCancellationRequested) + { + return false; + } + if (handle == IgnoreHandle || !NativeMethods.IsWindowVisible(handle) || (isWindow && NativeMethods.IsWindowCloaked(handle))) { return true; diff --git a/ShareX.UploadersLib/FileUploaders/SendSpace.cs b/ShareX.UploadersLib/FileUploaders/SendSpace.cs index a19f45cbc..37a917a56 100644 --- a/ShareX.UploadersLib/FileUploaders/SendSpace.cs +++ b/ShareX.UploadersLib/FileUploaders/SendSpace.cs @@ -619,10 +619,8 @@ public override string ToString() public void Dispose() { - if (cts != null) - { - cts.Cancel(); - } + cts?.Cancel(); + cts?.Dispose(); } }