@@ -12,17 +12,23 @@ public static class SingleInstance<TApplication>
1212 where TApplication : Application , ISingleInstance
1313 {
1414 private const string channelNameSufflix = ":SingeInstanceIPCChannel" ;
15- private static Mutex singleMutex ;
16- private static TinyMessageBus messageBus ;
15+ private static Mutex singleMutex ;
16+ private static TinyMessageBus messageBus ; //IPC message bus for communication between instances
1717
18+ /// <summary>
19+ /// Intended to be on app startup
20+ /// Initializes service if the call is from first instance
21+ /// Signals the first instance if it already exists
22+ /// </summary>
23+ /// <param name="uniqueName">A unique name for IPC channel</param>
24+ /// <returns>Whether the call is from application's first instance</returns>
1825 public static bool InitializeAsFirstInstance ( string uniqueName )
1926 {
2027 var CommandLineArgs = GetCommandLineArgs ( uniqueName ) ;
2128 string applicationIdentifier = uniqueName + Environment . UserName ;
22-
2329 string channelName = $ "{ applicationIdentifier } { channelNameSufflix } ";
24-
2530 singleMutex = new Mutex ( true , applicationIdentifier , out var firstInstance ) ;
31+
2632 if ( firstInstance )
2733 CreateRemoteService ( channelName ) ;
2834 else
@@ -39,8 +45,6 @@ private static void SignalFirstInstance(string channelName, IList<string> comman
3945 private static void CreateRemoteService ( string channelName )
4046 {
4147 messageBus = new TinyMessageBus ( channelName ) ;
42- messageBus . MessageReceived += ( _ , e ) =>
43- ( Application . Current as TApplication ) . OnInstanceInvoked ( e . Message . Deserialize < string [ ] > ( ) ) ;
4448 messageBus . MessageReceived += MessageBus_MessageReceived ;
4549 }
4650
@@ -67,12 +71,9 @@ private static string[] GetCommandLineArgs(string uniqueApplicationName)
6771 args = NativeMethods . CommandLineToArgvW ( reader . ReadToEnd ( ) ) ;
6872 File . Delete ( cmdLinePath ) ;
6973 }
70- catch ( IOException )
71- {
72- }
74+ catch ( IOException ) { }
7375 }
7476 }
75-
7677 if ( args == null )
7778 args = Array . Empty < string > ( ) ;
7879
0 commit comments