mirror of
https://github.com/ShareX/ShareX.git
synced 2024-06-28 03:00:22 +12:00
fixed #7329: SingleInstanceManager improvements
This commit is contained in:
parent
3b3c064514
commit
d64ae9702f
|
@ -33,6 +33,8 @@ public class DebugTimer : IDisposable
|
|||
{
|
||||
public string Text { get; set; }
|
||||
|
||||
public TimeSpan Elapsed => timer.Elapsed;
|
||||
|
||||
private Stopwatch timer;
|
||||
|
||||
public DebugTimer(string text = null)
|
||||
|
@ -60,12 +62,12 @@ private void Write(string time, string text = null)
|
|||
|
||||
public void WriteElapsedMilliseconds(string text = null)
|
||||
{
|
||||
Write(timer.Elapsed.TotalMilliseconds.ToString("0.000", CultureInfo.InvariantCulture) + " milliseconds.", text);
|
||||
Write(Elapsed.TotalMilliseconds.ToString("0.000", CultureInfo.InvariantCulture) + " milliseconds.", text);
|
||||
}
|
||||
|
||||
public void WriteElapsedSeconds(string text = null)
|
||||
{
|
||||
Write(timer.Elapsed.TotalSeconds.ToString("0.000", CultureInfo.InvariantCulture) + " seconds.", text);
|
||||
Write(Elapsed.TotalSeconds.ToString("0.000", CultureInfo.InvariantCulture) + " seconds.", text);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
|
|
@ -42,6 +42,7 @@ public class SingleInstanceManager : IDisposable
|
|||
public bool IsFirstInstance { get; private set; }
|
||||
|
||||
private const int MaxArgumentsLength = 100;
|
||||
private const int ConnectTimeout = 5000;
|
||||
|
||||
private readonly Mutex mutex;
|
||||
private CancellationTokenSource cts;
|
||||
|
@ -86,7 +87,10 @@ public SingleInstanceManager(string mutexName, string pipeName, bool isSingleIns
|
|||
|
||||
protected virtual void OnArgumentsReceived(string[] arguments)
|
||||
{
|
||||
ArgumentsReceived?.Invoke(arguments);
|
||||
if (ArgumentsReceived != null)
|
||||
{
|
||||
Task.Run(() => ArgumentsReceived?.Invoke(arguments));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ListenForConnectionsAsync()
|
||||
|
@ -97,7 +101,7 @@ private async Task ListenForConnectionsAsync()
|
|||
{
|
||||
using (NamedPipeServerStream serverPipe = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
|
||||
{
|
||||
await serverPipe.WaitForConnectionAsync(cts.Token);
|
||||
await serverPipe.WaitForConnectionAsync(cts.Token).ConfigureAwait(false);
|
||||
|
||||
using (BinaryReader reader = new BinaryReader(serverPipe, Encoding.UTF8))
|
||||
{
|
||||
|
@ -122,6 +126,12 @@ private async Task ListenForConnectionsAsync()
|
|||
catch when (cts.IsCancellationRequested)
|
||||
{
|
||||
}
|
||||
catch (UnauthorizedAccessException e)
|
||||
{
|
||||
DebugHelper.WriteException(e);
|
||||
|
||||
break;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugHelper.WriteException(e);
|
||||
|
@ -135,7 +145,7 @@ private void RedirectArgumentsToFirstInstance(string[] args)
|
|||
{
|
||||
using (NamedPipeClientStream clientPipe = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
|
||||
{
|
||||
clientPipe.Connect();
|
||||
clientPipe.Connect(ConnectTimeout);
|
||||
|
||||
using (BinaryWriter writer = new BinaryWriter(clientPipe, Encoding.UTF8))
|
||||
{
|
||||
|
|
|
@ -394,6 +394,19 @@ public static void Restart(bool asAdmin = false)
|
|||
|
||||
private static void SingleInstanceManager_ArgumentsReceived(string[] arguments)
|
||||
{
|
||||
string message = "Arguments received: ";
|
||||
|
||||
if (arguments == null)
|
||||
{
|
||||
message += "null";
|
||||
}
|
||||
else
|
||||
{
|
||||
message += "\"" + string.Join(" ", arguments) + "\"";
|
||||
}
|
||||
|
||||
DebugHelper.WriteLine(message);
|
||||
|
||||
if (WaitFormLoad(5000))
|
||||
{
|
||||
MainForm.InvokeSafe(async () =>
|
||||
|
|
Loading…
Reference in a new issue