11using System ;
2+ using System . Collections ;
23using System . Diagnostics ;
4+ using System . Diagnostics . CodeAnalysis ;
35using System . IO ;
46using System . IO . Pipes ;
57using System . Threading ;
68using System . Windows ;
79using Microsoft . VisualBasic . Logging ;
810using System . Windows . Interop ;
11+ using JetBrains . Annotations ;
912using Novus . Utilities ;
1013using Novus . Win32 ;
11-
14+ #nullable disable
1215namespace SmartImage . UI ;
1316
1417/// <summary>
1518/// Interaction logic for App.xaml
1619/// </summary>
1720public partial class App : Application
1821{
22+
1923 /// <summary>
2024 /// This identifier must be unique for each application.
2125 /// </summary>
22- public const string SingleGuid = "{910e8c27-ab31-4043-9c5d-1382707e6c93}" ;
26+ public const string SINGLE_GUID = "{910e8c27-ab31-4043-9c5d-1382707e6c93}" ;
2327
2428 public const string IPC_PIPE_NAME = "SIPC" ;
2529
2630 public const char ARGS_DELIM = '\0 ' ;
2731
28- private static Mutex SingleMutex ;
32+ private static Mutex _singleMutex ;
2933
3034 public NamedPipeServerStream PipeServer { get ; private set ; }
3135
3236 public Thread PipeThread { get ; private set ; }
3337
34- private void Application_Startup ( object sender , StartupEventArgs e )
38+ private void Application_Startup ( object sender , StartupEventArgs startupArgs )
3539 {
40+ bool multipleInstances = false , pipeServer = false ;
41+
42+ var enumerator = startupArgs . Args . GetEnumerator ( ) ;
43+ using var unknown = enumerator as IDisposable ;
44+
45+ while ( enumerator . MoveNext ( ) ) {
46+ var el = enumerator . Current ;
47+ var els = el ? . ToString ( ) ;
48+
49+ switch ( els ) {
50+ case "-mi" :
51+ multipleInstances = true ;
52+ break ;
53+ case "-ms" :
54+ pipeServer = true ;
55+ break ;
56+ default :
57+ break ;
58+ }
59+ }
3660
37- SingleMutex = new Mutex ( true , SingleGuid ) ;
38- var isOnlyInstance = SingleMutex . WaitOne ( TimeSpan . Zero , true ) ;
39-
40- var multipleInstances = false ;
61+ _singleMutex = new Mutex ( true , SINGLE_GUID ) ;
62+ var isOnlyInstance = _singleMutex . WaitOne ( TimeSpan . Zero , true ) ;
4163
4264 if ( multipleInstances || isOnlyInstance ) {
4365 // Show main window
4466 StartupUri = new Uri ( "MainWindow.xaml" , UriKind . Relative ) ;
4567
4668 // Release SingleInstance Mutex
47- SingleMutex . ReleaseMutex ( ) ;
48-
49- StartServer ( ) ;
69+ _singleMutex . ReleaseMutex ( ) ;
70+ if ( pipeServer ) {
71+ StartServer ( ) ;
5072
73+ }
5174 }
5275 else {
5376 // Bring the already running application into the foreground
5477 // Native.PostMessage(0xffff, AppUtil.m_registerWindowMessage, 0, 0);
55- SendMessage ( e ) ;
78+ SendMessage ( startupArgs ) ;
5679
5780 Shutdown ( ) ;
5881
5982 }
83+
6084 }
6185
6286 private static void SendMessage ( StartupEventArgs e )
6387 {
6488
65- using ( var pipe = new NamedPipeClientStream ( "." , IPC_PIPE_NAME , PipeDirection . Out ) )
66- using ( var stream = new StreamWriter ( pipe ) ) {
67- pipe . Connect ( ) ;
89+ using var pipe = new NamedPipeClientStream ( "." , IPC_PIPE_NAME , PipeDirection . Out ) ;
6890
69- foreach ( var s in e . Args ) {
70- stream . WriteLine ( s ) ;
71- }
91+ using var stream = new StreamWriter ( pipe ) ;
92+
93+ pipe . Connect ( ) ;
7294
73- stream . Write ( $ "{ ARGS_DELIM } { ProcessHelper . GetParent ( ) . Id } ") ;
95+ foreach ( var s in e . Args ) {
96+ stream . WriteLine ( s ) ;
7497 }
98+
99+ stream . Write ( $ "{ ARGS_DELIM } { ProcessHelper . GetParent ( ) . Id } ") ;
100+
75101 }
76102
77103 public delegate void PipeMessageCallback ( string s ) ;
@@ -102,4 +128,5 @@ private void StartServer()
102128 } ;
103129 PipeThread . Start ( ) ;
104130 }
131+
105132}
0 commit comments