File tree Expand file tree Collapse file tree 5 files changed +56
-2
lines changed
Expand file tree Collapse file tree 5 files changed +56
-2
lines changed Original file line number Diff line number Diff line change @@ -169,9 +169,9 @@ public static void SetTheme(string theme, string themeOverridesFile)
169169 }
170170 }
171171
172- public static void SetupTrayIcon ( bool enable )
172+ public void SetupTrayIcon ( bool enable )
173173 {
174- if ( enable )
174+ if ( enable && Native . OS . EnsureSingleInstance ( ) )
175175 {
176176 var icons = new TrayIcons {
177177 new TrayIcon {
@@ -186,6 +186,7 @@ public static void SetupTrayIcon(bool enable)
186186 } ;
187187 icons [ 0 ] . Clicked += ( _ , _ ) => ToggleWindow ( ) ;
188188 TrayIcon . SetIcons ( Current , icons ) ;
189+ _createdSystemTrayIcon = true ;
189190 }
190191 }
191192
@@ -591,5 +592,6 @@ private void ShowSelfUpdateResult(object data)
591592 private ResourceDictionary _activeLocale = null ;
592593 private ResourceDictionary _themeOverrides = null ;
593594 private ResourceDictionary _fontsOverrides = null ;
595+ private bool _createdSystemTrayIcon = false ;
594596 }
595597}
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ namespace SourceGit.Native
1111 [ SupportedOSPlatform ( "linux" ) ]
1212 internal class Linux : OS . IBackend
1313 {
14+ private FileStream _fs = null ;
1415 public void SetupApp ( AppBuilder builder )
1516 {
1617 builder . With ( new X11PlatformOptions ( ) { EnableIme = true } ) ;
@@ -97,6 +98,26 @@ public void OpenWithDefaultEditor(string file)
9798 }
9899 }
99100
101+ public bool EnsureSingleInstance ( )
102+ {
103+ var pidfile = Path . Combine ( Path . GetTempPath ( ) , "sourcegit.pid" ) ;
104+ var pid = Process . GetCurrentProcess ( ) . Id . ToString ( ) ;
105+ Console . WriteLine ( "pid " + pid ) ;
106+
107+ try
108+ {
109+ _fs = File . OpenWrite ( pidfile ) ;
110+ _fs . Lock ( 0 , 1000 ) ;
111+ new StreamWriter ( _fs ) . Write ( pid ) ;
112+ return true ;
113+ }
114+ catch ( IOException )
115+ {
116+ Console . WriteLine ( "another SourceGit is running" ) ;
117+ return false ;
118+ }
119+ }
120+
100121 private string FindExecutable ( string filename )
101122 {
102123 var pathVariable = Environment . GetEnvironmentVariable ( "PATH" ) ?? string . Empty ;
Original file line number Diff line number Diff line change @@ -86,5 +86,7 @@ public void OpenWithDefaultEditor(string file)
8686 {
8787 Process . Start ( "open" , $ "\" { file } \" ") ;
8888 }
89+
90+ public bool EnsureSingleInstance ( ) { return true ; }
8991 }
9092}
Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ public interface IBackend
2323 void OpenInFileManager ( string path , bool select ) ;
2424 void OpenBrowser ( string url ) ;
2525 void OpenWithDefaultEditor ( string file ) ;
26+
27+ bool EnsureSingleInstance ( ) ;
2628 }
2729
2830 public static string DataDir {
@@ -217,6 +219,11 @@ private static void UpdateGitVersion()
217219 [ GeneratedRegex ( @"^git version[\s\w]*(\d+)\.(\d+)[\.\-](\d+).*$" ) ]
218220 private static partial Regex REG_GIT_VERSION ( ) ;
219221
222+ public static bool EnsureSingleInstance ( )
223+ {
224+ return _backend . EnsureSingleInstance ( ) ;
225+ }
226+
220227 private static IBackend _backend = null ;
221228 private static string _gitExecutable = string . Empty ;
222229 }
Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ namespace SourceGit.Native
1414 [ SupportedOSPlatform ( "windows" ) ]
1515 internal class Windows : OS . IBackend
1616 {
17+ private FileStream _fs = null ;
18+
1719 [ StructLayout ( LayoutKind . Sequential ) ]
1820 internal struct RTL_OSVERSIONINFOEX
1921 {
@@ -393,5 +395,25 @@ private string FindVSSolutionFile(DirectoryInfo dir, int leftDepth)
393395
394396 return null ;
395397 }
398+
399+ public bool EnsureSingleInstance ( )
400+ {
401+ var pidfile = Path . Combine ( Path . GetTempPath ( ) , "sourcegit.pid" ) ;
402+ var pid = Process . GetCurrentProcess ( ) . Id . ToString ( ) ;
403+ Console . WriteLine ( "pid " + pid ) ;
404+
405+ try
406+ {
407+ _fs = File . OpenWrite ( pidfile ) ;
408+ _fs . Lock ( 0 , 1000 ) ;
409+ new StreamWriter ( _fs ) . Write ( pid ) ;
410+ return true ;
411+ }
412+ catch ( IOException )
413+ {
414+ Console . WriteLine ( "another SourceGit is running" ) ;
415+ return false ;
416+ }
417+ }
396418 }
397419}
You can’t perform that action at this time.
0 commit comments