fixed #7329: SingleInstanceManager improvements

This commit is contained in:
Jaex 2024-03-20 18:37:51 +03:00
parent 3b3c064514
commit d64ae9702f
3 changed files with 30 additions and 5 deletions

View File

@ -33,6 +33,8 @@ namespace ShareX.HelpersLib
{
public string Text { get; set; }
public TimeSpan Elapsed => timer.Elapsed;
private Stopwatch timer;
public DebugTimer(string text = null)
@ -60,12 +62,12 @@ namespace ShareX.HelpersLib
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()

View File

@ -42,6 +42,7 @@ namespace ShareX.HelpersLib
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 @@ namespace ShareX.HelpersLib
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 @@ namespace ShareX.HelpersLib
{
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 @@ namespace ShareX.HelpersLib
catch when (cts.IsCancellationRequested)
{
}
catch (UnauthorizedAccessException e)
{
DebugHelper.WriteException(e);
break;
}
catch (Exception e)
{
DebugHelper.WriteException(e);
@ -135,7 +145,7 @@ namespace ShareX.HelpersLib
{
using (NamedPipeClientStream clientPipe = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
{
clientPipe.Connect();
clientPipe.Connect(ConnectTimeout);
using (BinaryWriter writer = new BinaryWriter(clientPipe, Encoding.UTF8))
{

View File

@ -394,6 +394,19 @@ namespace ShareX
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 () =>