mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-28 15:31:30 +12:00
Code refactoring
This commit is contained in:
parent
bedd13c597
commit
856a1a057c
6 changed files with 73 additions and 72 deletions
|
@ -55,19 +55,22 @@ public async Task<string> Start(string filePath, HashType hashType)
|
|||
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 @@ public async Task<string> Start(string filePath, HashType hashType)
|
|||
|
||||
public void Stop()
|
||||
{
|
||||
if (cts != null)
|
||||
{
|
||||
cts.Cancel();
|
||||
}
|
||||
cts?.Cancel();
|
||||
}
|
||||
|
||||
private string HashCheckThread(string filePath, HashType hashType, IProgress<float> progress, CancellationToken ct)
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,21 +51,21 @@ public async Task Run(Action action)
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,42 +35,35 @@ public class WindowsRectangleList
|
|||
{
|
||||
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 @@ 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;
|
||||
|
|
|
@ -619,10 +619,8 @@ public override string ToString()
|
|||
|
||||
public void Dispose()
|
||||
{
|
||||
if (cts != null)
|
||||
{
|
||||
cts.Cancel();
|
||||
}
|
||||
cts?.Cancel();
|
||||
cts?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue