22using System . Linq ;
33using System . Collections . Generic ;
44using System . IO ;
5+ using System . Diagnostics ;
6+ using System . Reflection ;
7+ using System . Threading ;
58using System . Threading . Tasks ;
69using System . Windows ;
710using System . Windows . Controls ;
@@ -31,6 +34,7 @@ namespace XamlViewer
3134 public partial class App : PrismApplication
3235 {
3336 private string [ ] _xamlFiles = null ;
37+ private Mutex _singleMutex = null ;
3438
3539 public App ( )
3640 : base ( )
@@ -70,17 +74,58 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE
7074
7175 #endregion
7276
77+ public static Process GetRunningInstance ( )
78+ {
79+ var current = Process . GetCurrentProcess ( ) ;
80+ var processes = Process . GetProcessesByName ( current . ProcessName ) ;
81+
82+ foreach ( Process process in processes )
83+ {
84+ if ( process . Id != current . Id )
85+ {
86+ var processLocation = Assembly . GetExecutingAssembly ( ) . Location . Replace ( "/" , "//" ) ;
87+
88+ if ( Path . GetDirectoryName ( processLocation ) + "\\ " + Path . GetFileNameWithoutExtension ( processLocation )
89+ == Path . GetDirectoryName ( current . MainModule . FileName ) + "\\ " + Path . GetFileNameWithoutExtension ( current . MainModule . FileName ) )
90+ return process ;
91+ }
92+ }
93+
94+ return null ;
95+ }
96+
7397 protected override void OnStartup ( StartupEventArgs e )
74- {
75- if ( e . Args . Length > 0 )
98+ {
99+ _singleMutex = new Mutex ( true , "huangjia2107_XAML_VIEWER" , out bool isNew ) ;
100+ var process = GetRunningInstance ( ) ;
101+
102+ if ( ! isNew && process != null )
103+ {
104+ Win32 . ShowWindowAsync ( process . MainWindowHandle ) ;
105+ Win32 . SetForegroundWindow ( process . MainWindowHandle ) ;
106+
107+ if ( e . Args . Length > 0 )
108+ {
109+ var xamls = e . Args . Where ( f => Path . GetExtension ( f ) . ToLower ( ) == ".xaml" ) . ToArray ( ) ;
110+ if ( xamls != null && xamls . Length > 0 )
111+ {
112+ //TODO:Send Message...
113+ }
114+ }
115+
116+ Environment . Exit ( 0 ) ;
117+ return ;
118+ }
119+
120+ if ( e . Args . Length > 0 )
76121 {
77122 _xamlFiles = e . Args . Where ( f => Path . GetExtension ( f ) . ToLower ( ) == ".xaml" ) . ToArray ( ) ;
78123 if ( _xamlFiles == null || _xamlFiles . Length == 0 )
79124 {
80125 Environment . Exit ( 0 ) ;
81126 return ;
82127 }
83- }
128+ }
84129
85130 base . OnStartup ( e ) ;
86131 }
@@ -153,10 +198,10 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
153198 appData . Config . Files . RemoveAll ( f => _xamlFiles . Any ( xf => Path . GetFullPath ( xf ) . ToLower ( ) == Path . GetFullPath ( f ) . ToLower ( ) ) ) ;
154199 appData . Config . Files . InsertRange ( 0 , _xamlFiles ) ;
155200 }
156-
157- //Data Source
158- if ( FileHelper . Exists ( ResourcesMap . LocationDic [ Location . DataSourceFile ] ) )
159- appData . Config . DataSourceJsonString = FileHelper . LoadFromFile ( ResourcesMap . LocationDic [ Location . DataSourceFile ] ) ;
201+
202+ //Data Source
203+ if ( FileHelper . Exists ( ResourcesMap . LocationDic [ Location . DataSourceFile ] ) )
204+ appData . Config . DataSourceJsonString = FileHelper . LoadFromFile ( ResourcesMap . LocationDic [ Location . DataSourceFile ] ) ;
160205
161206 containerRegistry . RegisterInstance ( appData ) ;
162207 }
@@ -167,16 +212,16 @@ protected override IModuleCatalog CreateModuleCatalog()
167212 }
168213
169214 protected override void OnInitialized ( )
170- {
215+ {
171216 base . OnInitialized ( ) ;
172-
217+
173218 var eventAggregator = Container . Resolve < IEventAggregator > ( ) ;
174- var appData = Container . Resolve < AppData > ( ) ;
175-
219+ var appData = Container . Resolve < AppData > ( ) ;
220+
176221 eventAggregator ? . GetEvent < InitWorkAreaEvent > ( ) . Publish ( ) ;
177-
178- if ( appData . Config . IsSyncDataSource )
179- eventAggregator ? . GetEvent < SyncDataSourceEvent > ( ) . Publish ( appData . Config . DataSourceJsonString ? . Trim ( ) ) ;
222+
223+ if ( appData . Config . IsSyncDataSource )
224+ eventAggregator ? . GetEvent < SyncDataSourceEvent > ( ) . Publish ( appData . Config . DataSourceJsonString ? . Trim ( ) ) ;
180225 }
181226 }
182227}
0 commit comments