refactor: use lock file instead of named mutex since the second one may not work on other platforms

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-04-15 14:36:12 +08:00
parent 9ebde1943e
commit 90b37663ed
No known key found for this signature in database

View file

@ -19,23 +19,20 @@ namespace SourceGit.Models
{ {
try try
{ {
_singletonMutex = new Mutex(false, "SourceGit_2994509B-4906-4A48-9A45-55C1836A8208", out _isFirstInstance); _singletoneLock = File.Open(Path.Combine(Native.OS.DataDir, "process.lock"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
_isFirstInstance = true;
if (_isFirstInstance) _server = new NamedPipeServerStream(
{ "SourceGitIPCChannel",
_server = new NamedPipeServerStream( PipeDirection.In,
"SourceGitIPCChannel", -1,
PipeDirection.In, PipeTransmissionMode.Byte,
-1, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);
PipeTransmissionMode.Byte, _cancellationTokenSource = new CancellationTokenSource();
PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly); Task.Run(StartServer);
_cancellationTokenSource = new CancellationTokenSource();
Task.Run(StartServer);
}
} }
catch catch
{ {
// IGNORE _isFirstInstance = false;
} }
} }
@ -70,7 +67,7 @@ namespace SourceGit.Models
public void Dispose() public void Dispose()
{ {
_cancellationTokenSource?.Cancel(); _cancellationTokenSource?.Cancel();
_singletonMutex.Dispose(); _singletoneLock?.Dispose();
} }
private async void StartServer() private async void StartServer()
@ -99,7 +96,7 @@ namespace SourceGit.Models
} }
} }
private Mutex _singletonMutex = null; private FileStream _singletoneLock = null;
private bool _isFirstInstance = false; private bool _isFirstInstance = false;
private NamedPipeServerStream _server = null; private NamedPipeServerStream _server = null;
private CancellationTokenSource _cancellationTokenSource = null; private CancellationTokenSource _cancellationTokenSource = null;