Code refactoring

This commit is contained in:
Jaex 2024-02-12 02:50:51 +03:00
parent bedd13c597
commit 856a1a057c
6 changed files with 73 additions and 72 deletions

View File

@ -55,19 +55,22 @@ namespace ShareX.HelpersLib
IsWorking = true;
Progress<float> progress = new Progress<float>(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 @@ namespace ShareX.HelpersLib
public void Stop()
{
if (cts != null)
{
cts.Cancel();
}
cts?.Cancel();
}
private string HashCheckThread(string filePath, HashType hashType, IProgress<float> progress, CancellationToken ct)

View File

@ -44,7 +44,7 @@ namespace ShareX.HelpersLib
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 @@ namespace ShareX.HelpersLib
if (IsFirstInstance)
{
Task.Run(() => ListenForConnectionsAsync());
cts = new CancellationTokenSource();
Task.Run(() => ListenForConnectionsAsync(), cts.Token);
}
else
{
@ -89,15 +91,13 @@ namespace ShareX.HelpersLib
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 @@ namespace ShareX.HelpersLib
}
}
}
catch (Exception e) when (cancellationSource.IsCancellationRequested)
catch when (cts.IsCancellationRequested)
{
}
catch (Exception e)
@ -144,7 +144,8 @@ namespace ShareX.HelpersLib
public void Dispose()
{
cancellationSource?.Cancel();
cts?.Cancel();
cts?.Dispose();
mutex?.ReleaseMutex();
}
}

View File

@ -51,21 +51,21 @@ namespace ShareX.HelpersLib
IsCanceled = false;
p = new Progress<T>(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;
}
}
}

View File

@ -335,10 +335,14 @@ namespace ShareX.ScreenCaptureLib
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();
});
}
}

View File

@ -35,42 +35,35 @@ namespace ShareX.ScreenCaptureLib
{
public IntPtr IgnoreHandle { get; set; }
public bool IncludeChildWindows { get; set; }
public int Timeout { get; set; }
private List<SimpleWindowInfo> windows;
private HashSet<IntPtr> parentHandles;
public List<SimpleWindowInfo> GetWindowInfoListAsync(int timeout)
{
List<SimpleWindowInfo> 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<SimpleWindowInfo> GetWindowInfoList()
{
windows = new List<SimpleWindowInfo>();
parentHandles = new HashSet<IntPtr>();
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<SimpleWindowInfo> result = new List<SimpleWindowInfo>();
@ -111,6 +104,11 @@ namespace ShareX.ScreenCaptureLib
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;

View File

@ -619,10 +619,8 @@ namespace ShareX.UploadersLib.FileUploaders
public void Dispose()
{
if (cts != null)
{
cts.Cancel();
}
cts?.Cancel();
cts?.Dispose();
}
}