33using System . IO ;
44using System . Text ;
55using System . Threading ;
6- using System . Windows ;
76using TinyIpc . Messaging ;
87
98namespace SingleInstanceCore
109{
11- public static class SingleInstance < TApplication >
12- where TApplication : Application , ISingleInstance
10+ public static class SingleInstance
1311 {
1412 private const string channelNameSufflix = ":SingeInstanceIPCChannel" ;
13+ //For detecting if mutex is locked (first instance is already up then)
1514 private static Mutex singleMutex ;
16- private static TinyMessageBus messageBus ; //IPC message bus for communication between instances
15+ //Message bus for communication between instances
16+ private static TinyMessageBus messageBus ;
1717
1818 /// <summary>
1919 /// Intended to be on app startup
@@ -22,34 +22,35 @@ public static class SingleInstance<TApplication>
2222 /// </summary>
2323 /// <param name="uniqueName">A unique name for IPC channel</param>
2424 /// <returns>Whether the call is from application's first instance</returns>
25- public static bool InitializeAsFirstInstance ( string uniqueName )
25+ public static bool InitializeAsFirstInstance < T > ( this T instance , string uniqueName ) where T : ISingleInstance
2626 {
2727 var CommandLineArgs = GetCommandLineArgs ( uniqueName ) ;
2828 var applicationIdentifier = uniqueName + Environment . UserName ;
2929 var channelName = $ "{ applicationIdentifier } { channelNameSufflix } ";
3030 singleMutex = new Mutex ( true , applicationIdentifier , out var firstInstance ) ;
3131
3232 if ( firstInstance )
33- CreateRemoteService ( channelName ) ;
33+ CreateRemoteService ( instance , channelName ) ;
3434 else
3535 SignalFirstInstance ( channelName , CommandLineArgs ) ;
3636
3737 return firstInstance ;
3838 }
3939
40- private static void SignalFirstInstance ( string channelName , IList < string > commandLineArgs ) => new TinyMessageBus ( channelName ) . PublishAsync ( commandLineArgs . Serialize ( ) ) ;
41-
42- private static void CreateRemoteService ( string channelName )
40+ private static void SignalFirstInstance ( string channelName , IList < string > commandLineArgs )
4341 {
44- messageBus = new TinyMessageBus ( channelName ) ;
45- messageBus . MessageReceived += MessageBus_MessageReceived ;
42+ var bus = new TinyMessageBus ( channelName ) ;
43+ var serializedArgs = commandLineArgs . Serialize ( ) ;
44+ bus . PublishAsync ( serializedArgs ) ;
4645 }
4746
48- private static void MessageBus_MessageReceived ( object sender , TinyMessageReceivedEventArgs e )
47+ private static void CreateRemoteService ( ISingleInstance instance , string channelName )
4948 {
50- var app = Application . Current as TApplication ;
51- var args = e . Message . Deserialize < string [ ] > ( ) ;
52- app . OnInstanceInvoked ( args ) ;
49+ messageBus = new TinyMessageBus ( channelName ) ;
50+ messageBus . MessageReceived += ( _ , e ) =>
51+ {
52+ instance . OnInstanceInvoked ( e . Message . Deserialize < string [ ] > ( ) ) ;
53+ } ;
5354 }
5455
5556 private static string [ ] GetCommandLineArgs ( string uniqueApplicationName )
0 commit comments