@@ -10,7 +10,7 @@ public class IpcChannel : IDisposable
10
10
{
11
11
public bool IsFirstInstance
12
12
{
13
- get => _server != null ;
13
+ get => _isFirstInstance ;
14
14
}
15
15
16
16
public event Action < string > MessageReceived ;
@@ -19,14 +19,19 @@ public IpcChannel()
19
19
{
20
20
try
21
21
{
22
- _server = new NamedPipeServerStream (
23
- "SourceGitIPCChannel" ,
24
- PipeDirection . In ,
25
- 1 ,
26
- PipeTransmissionMode . Byte ,
27
- PipeOptions . Asynchronous | PipeOptions . CurrentUserOnly | PipeOptions . FirstPipeInstance ) ;
28
- _cancellationTokenSource = new CancellationTokenSource ( ) ;
29
- Task . Run ( StartServer ) ;
22
+ _singletonMutex = new Mutex ( false , "SourceGit_2994509B-4906-4A48-9A45-55C1836A8208" , out _isFirstInstance ) ;
23
+
24
+ if ( _isFirstInstance )
25
+ {
26
+ _server = new NamedPipeServerStream (
27
+ "SourceGitIPCChannel" ,
28
+ PipeDirection . In ,
29
+ - 1 ,
30
+ PipeTransmissionMode . Byte ,
31
+ PipeOptions . Asynchronous | PipeOptions . CurrentUserOnly ) ;
32
+ _cancellationTokenSource = new CancellationTokenSource ( ) ;
33
+ Task . Run ( StartServer ) ;
34
+ }
30
35
}
31
36
catch
32
37
{
@@ -60,6 +65,7 @@ public void SendToFirstInstance(string cmd)
60
65
public void Dispose ( )
61
66
{
62
67
_cancellationTokenSource ? . Cancel ( ) ;
68
+ _singletonMutex . Dispose ( ) ;
63
69
}
64
70
65
71
private async void StartServer ( )
@@ -83,6 +89,8 @@ private async void StartServer()
83
89
}
84
90
}
85
91
92
+ private Mutex _singletonMutex = null ;
93
+ private bool _isFirstInstance = false ;
86
94
private NamedPipeServerStream _server = null ;
87
95
private CancellationTokenSource _cancellationTokenSource = null ;
88
96
}
0 commit comments