9
9
using System . Diagnostics ;
10
10
using System . Drawing ; // for notifyicon
11
11
using System . IO ;
12
+ using System . IO . Pipes ;
12
13
using System . Runtime . InteropServices ;
13
14
using System . Text . RegularExpressions ;
14
15
using System . Threading ;
@@ -70,6 +71,9 @@ public partial class MainWindow : Window
70
71
List < BuildReport > buildReports = new List < BuildReport > ( ) ; // multiple reports, each contains their own stats and items
71
72
int currentBuildReport = 0 ;
72
73
74
+ private NamedPipeServerStream pipeServer ;
75
+ private const string PipeName = appName ;
76
+
73
77
[ DllImport ( "user32" , CharSet = CharSet . Unicode ) ]
74
78
static extern IntPtr FindWindow ( string cls , string win ) ;
75
79
[ DllImport ( "user32" ) ]
@@ -118,14 +122,22 @@ void Start()
118
122
// check for duplicate instance, and activate that instead
119
123
if ( chkAllowSingleInstanceOnly . IsChecked == true )
120
124
{
121
- bool aIsNewInstance = false ;
122
- myMutex = new Mutex ( true , appName , out aIsNewInstance ) ;
123
- if ( ! aIsNewInstance )
125
+ bool isNewInstance ;
126
+ myMutex = new Mutex ( true , appName , out isNewInstance ) ;
127
+
128
+ if ( ! isNewInstance )
124
129
{
125
- // NOTE doesnt work if its minized to tray
126
- ActivateOtherWindow ( ) ;
130
+ // Send a wake-up message to the running instance
131
+ ActivateRunningInstance ( ) ;
132
+
133
+ // Exit the current instance
127
134
App . Current . Shutdown ( ) ;
128
135
}
136
+ else
137
+ {
138
+ // Start pipe server in the first instance
139
+ StartPipeServer ( ) ;
140
+ }
129
141
}
130
142
131
143
// TEST erase custom history data
@@ -134,7 +146,7 @@ void Start()
134
146
135
147
projectsSource = GetProjects . Scan ( getGitBranch : ( bool ) chkShowGitBranchColumn . IsChecked , getPlasticBranch : ( bool ) chkCheckPlasticBranch . IsChecked , getArguments : ( bool ) chkShowLauncherArgumentsColumn . IsChecked , showMissingFolders : ( bool ) chkShowMissingFolderProjects . IsChecked , showTargetPlatform : ( bool ) chkShowPlatform . IsChecked , AllProjectPaths : Properties . Settings . Default . projectPaths , searchGitbranchRecursivly : ( bool ) chkGetGitBranchRecursively . IsChecked ) ;
136
148
137
- Console . WriteLine ( "projectsSource.Count: " + projectsSource . Count ) ;
149
+ // Console.WriteLine("projectsSource.Count: " + projectsSource.Count);
138
150
139
151
gridRecent . Items . Clear ( ) ;
140
152
gridRecent . ItemsSource = projectsSource ;
@@ -825,6 +837,11 @@ public void RefreshRecentProjects()
825
837
826
838
// maximize window
827
839
void NotifyIcon_MouseClick ( object sender , System . Windows . Forms . MouseEventArgs e )
840
+ {
841
+ RestoreFromTray ( ) ;
842
+ }
843
+
844
+ void RestoreFromTray ( )
828
845
{
829
846
this . Show ( ) ;
830
847
this . WindowState = WindowState . Normal ;
@@ -2938,7 +2955,7 @@ public void ProcessExitedCallBack(Project proj)
2938
2955
gridRecent . CancelEdit ( DataGridEditingUnit . Cell ) ;
2939
2956
}
2940
2957
2941
- // FIXME nobody likes extra loops.. but only 40 items to find correct project? but still..
2958
+ // FIXME nobody likes extra loops.. but only # items to find correct project? but still..
2942
2959
for ( int i = 0 , len = projectsSource . Count ; i < len ; i ++ )
2943
2960
{
2944
2961
if ( projectsSource [ i ] . Path == proj . Path )
@@ -3680,6 +3697,66 @@ private void UseAlphaReleaseNotes_Checked(object sender, RoutedEventArgs e)
3680
3697
Settings . Default . Save ( ) ;
3681
3698
}
3682
3699
3700
+ private void ActivateRunningInstance ( )
3701
+ {
3702
+ try
3703
+ {
3704
+ using ( var pipeClient = new NamedPipeClientStream ( "." , PipeName , PipeDirection . Out ) )
3705
+ {
3706
+ pipeClient . Connect ( 1000 ) ; // Wait for 1 second to connect
3707
+ using ( var writer = new StreamWriter ( pipeClient ) )
3708
+ {
3709
+ writer . WriteLine ( "WakeUp" ) ;
3710
+ writer . Flush ( ) ;
3711
+ }
3712
+ }
3713
+ }
3714
+ catch ( Exception ex )
3715
+ {
3716
+ // Handle connection failure (e.g., pipe not available)
3717
+ Console . WriteLine ( "Could not connect to the running instance: " + ex . Message ) ;
3718
+ }
3719
+ }
3720
+
3721
+ private void StartPipeServer ( )
3722
+ {
3723
+ pipeServer = new NamedPipeServerStream ( PipeName , PipeDirection . In , 1 , PipeTransmissionMode . Message , PipeOptions . Asynchronous ) ;
3724
+ pipeServer . BeginWaitForConnection ( OnPipeConnection , null ) ;
3725
+ }
3726
+
3727
+ private void OnPipeConnection ( IAsyncResult result )
3728
+ {
3729
+ try
3730
+ {
3731
+ pipeServer . EndWaitForConnection ( result ) ;
3732
+
3733
+ // Read the message
3734
+ using ( var reader = new StreamReader ( pipeServer ) )
3735
+ {
3736
+ string message = reader . ReadLine ( ) ;
3737
+ if ( message == "WakeUp" )
3738
+ {
3739
+ Dispatcher . Invoke ( ( ) =>
3740
+ {
3741
+ // Bring the app to the foreground
3742
+ RestoreFromTray ( ) ;
3743
+ } ) ;
3744
+ }
3745
+ }
3746
+ }
3747
+ catch ( Exception ex )
3748
+ {
3749
+ // Handle exceptions
3750
+ Console . WriteLine ( ex ) ;
3751
+ }
3752
+ finally
3753
+ {
3754
+ // Restart pipe server to listen for new messages
3755
+ StartPipeServer ( ) ;
3756
+ }
3757
+ }
3758
+
3759
+
3683
3760
//private void menuProjectProperties_Click(object sender, RoutedEventArgs e)
3684
3761
//{
3685
3762
// var proj = GetSelectedProject();
0 commit comments