mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-20 19:55:00 +00:00
enhance: use Mutex
to force running SourceGit
in singleton mode
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
parent
0acbe3e487
commit
e2da44c8fd
1 changed files with 17 additions and 9 deletions
|
@ -10,7 +10,7 @@ namespace SourceGit.Models
|
||||||
{
|
{
|
||||||
public bool IsFirstInstance
|
public bool IsFirstInstance
|
||||||
{
|
{
|
||||||
get => _server != null;
|
get => _isFirstInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public event Action<string> MessageReceived;
|
public event Action<string> MessageReceived;
|
||||||
|
@ -18,16 +18,21 @@ namespace SourceGit.Models
|
||||||
public IpcChannel()
|
public IpcChannel()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
_singletonMutex = new Mutex(false, "SourceGit_2994509B-4906-4A48-9A45-55C1836A8208", out _isFirstInstance);
|
||||||
|
|
||||||
|
if (_isFirstInstance)
|
||||||
{
|
{
|
||||||
_server = new NamedPipeServerStream(
|
_server = new NamedPipeServerStream(
|
||||||
"SourceGitIPCChannel",
|
"SourceGitIPCChannel",
|
||||||
PipeDirection.In,
|
PipeDirection.In,
|
||||||
1,
|
-1,
|
||||||
PipeTransmissionMode.Byte,
|
PipeTransmissionMode.Byte,
|
||||||
PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly | PipeOptions.FirstPipeInstance);
|
PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);
|
||||||
_cancellationTokenSource = new CancellationTokenSource();
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
Task.Run(StartServer);
|
Task.Run(StartServer);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// IGNORE
|
// IGNORE
|
||||||
|
@ -60,6 +65,7 @@ namespace SourceGit.Models
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_cancellationTokenSource?.Cancel();
|
_cancellationTokenSource?.Cancel();
|
||||||
|
_singletonMutex.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void StartServer()
|
private async void StartServer()
|
||||||
|
@ -83,6 +89,8 @@ namespace SourceGit.Models
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Mutex _singletonMutex = null;
|
||||||
|
private bool _isFirstInstance = false;
|
||||||
private NamedPipeServerStream _server = null;
|
private NamedPipeServerStream _server = null;
|
||||||
private CancellationTokenSource _cancellationTokenSource = null;
|
private CancellationTokenSource _cancellationTokenSource = null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue