-
Notifications
You must be signed in to change notification settings - Fork 2
Usage
Bruce Wayne edited this page Nov 9, 2021
·
3 revisions
const string identifier = @"Global\SingleInstance" // Change to your Guid or something unique
ISingleInstanceService singleInstance = new SingleInstanceService(identifier);
// You should create service as singleton!
if(singleInstance.TryStartSingleInstance())
{
// Success
}
else
{
// There is another program register as same the identifier
}
if(singleInstance.IsFirstInstance)
{
// This is the first instance.
}
else
{
// This is not the first instance.
}
if(singleInstance.IsFirstInstance)
{
singleInstance.StartListenServer();
}
singleInstance.Received.SelectMany(ServerResponseAsync).Subscribe(); //Async
async Task<Unit> ServerResponseAsync((string, Action<string>) receive)
{
var (message, endFunc) = receive;
// TODO handle message
await Task.Delay(TimeSpan.FromSeconds(3)); // You may do some jobs
endFunc("success"); // Send response
return default;
}
ISingleInstanceService singleInstance2 = new SingleInstanceService(identifier);
string message; // Your message
string response = await singleInstance2.SendMessageToFirstInstanceAsync(message);
// TODO handle response
singleInstance.Dispose();