Initial commit

This commit is contained in:
2023-04-07 10:38:17 -04:00
parent 1a1c28196d
commit 1c7bc2bb50
4 changed files with 120 additions and 0 deletions

67
SingleInstance.cs Normal file
View File

@@ -0,0 +1,67 @@
using H.Pipes;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ChrisKaczor.Wpf.Application
{
public static class SingleInstance
{
public static event EventHandler<string> MessageReceived = delegate { };
private static PipeServer<string>? _pipeServer;
private static SynchronizationContext? _syncContext;
public static async Task<IDisposable?> GetSingleInstanceHandleAsync(string applicationName)
{
// Create the mutex and see if it was newly created
var mutex = new Mutex(false, applicationName, out var createdNew);
// Return the mutex if it was created
if (createdNew) return mutex;
// Create a client to send to the other instance
await using var pipeClient = new PipeClient<string>(applicationName);
try
{
// Connect to the other instance
await pipeClient.ConnectAsync(new CancellationTokenSource(500).Token);
// Send the command line to the other instance
await pipeClient.WriteAsync(Environment.CommandLine);
}
catch (Exception)
{
// Ignored
}
return null;
}
public static async Task StartAsync(string applicationName)
{
// Store the synchronization context of the current thread
_syncContext = SynchronizationContext.Current;
_pipeServer = new PipeServer<string>(applicationName);
_pipeServer.MessageReceived += PipeServer_MessageReceived;
await _pipeServer.StartAsync();
}
public static async Task Stop()
{
if (_pipeServer == null) return;
await _pipeServer.StopAsync();
}
private static void PipeServer_MessageReceived(object? sender, H.Pipes.Args.ConnectionMessageEventArgs<string?> e)
{
// Fire the event on the original thread
_syncContext?.Send(_ => MessageReceived(null, e.Message!), null);
}
}
}