Skip to content

Commit f7f3903

Browse files
committed
Use IPC pipe messages to restore existing tray minimized instance, fixes #67
1 parent 8c7d355 commit f7f3903

File tree

1 file changed

+84
-7
lines changed

1 file changed

+84
-7
lines changed

UnityLauncherPro/MainWindow.xaml.cs

+84-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Diagnostics;
1010
using System.Drawing; // for notifyicon
1111
using System.IO;
12+
using System.IO.Pipes;
1213
using System.Runtime.InteropServices;
1314
using System.Text.RegularExpressions;
1415
using System.Threading;
@@ -70,6 +71,9 @@ public partial class MainWindow : Window
7071
List<BuildReport> buildReports = new List<BuildReport>(); // multiple reports, each contains their own stats and items
7172
int currentBuildReport = 0;
7273

74+
private NamedPipeServerStream pipeServer;
75+
private const string PipeName = appName;
76+
7377
[DllImport("user32", CharSet = CharSet.Unicode)]
7478
static extern IntPtr FindWindow(string cls, string win);
7579
[DllImport("user32")]
@@ -118,14 +122,22 @@ void Start()
118122
// check for duplicate instance, and activate that instead
119123
if (chkAllowSingleInstanceOnly.IsChecked == true)
120124
{
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)
124129
{
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
127134
App.Current.Shutdown();
128135
}
136+
else
137+
{
138+
// Start pipe server in the first instance
139+
StartPipeServer();
140+
}
129141
}
130142

131143
// TEST erase custom history data
@@ -134,7 +146,7 @@ void Start()
134146

135147
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);
136148

137-
Console.WriteLine("projectsSource.Count: " + projectsSource.Count);
149+
//Console.WriteLine("projectsSource.Count: " + projectsSource.Count);
138150

139151
gridRecent.Items.Clear();
140152
gridRecent.ItemsSource = projectsSource;
@@ -825,6 +837,11 @@ public void RefreshRecentProjects()
825837

826838
// maximize window
827839
void NotifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
840+
{
841+
RestoreFromTray();
842+
}
843+
844+
void RestoreFromTray()
828845
{
829846
this.Show();
830847
this.WindowState = WindowState.Normal;
@@ -2938,7 +2955,7 @@ public void ProcessExitedCallBack(Project proj)
29382955
gridRecent.CancelEdit(DataGridEditingUnit.Cell);
29392956
}
29402957

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..
29422959
for (int i = 0, len = projectsSource.Count; i < len; i++)
29432960
{
29442961
if (projectsSource[i].Path == proj.Path)
@@ -3680,6 +3697,66 @@ private void UseAlphaReleaseNotes_Checked(object sender, RoutedEventArgs e)
36803697
Settings.Default.Save();
36813698
}
36823699

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+
36833760
//private void menuProjectProperties_Click(object sender, RoutedEventArgs e)
36843761
//{
36853762
// var proj = GetSelectedProject();

0 commit comments

Comments
 (0)