• 0

[C#] Restoring an app from the systray


Question

I have a small application that minimizes itself to the system tray, but I also want to ensure that there is only ever a single instance of it. When the first instance isn't minimized, and I attempt to start another instance, my code brings the app to the front as it should. However, when the app is minimized to the system tray, no combinations of SW_SHOW, SW_RESTORE, etc are able to restore the window.

To minimize to the tray, I'm using the following code:

private void Form1_SizeChanged(Object sender, System.EventArgs e) {
	if(shown && this.WindowState == FormWindowState.Minimized) {
  shown = false;
  this.ShowInTaskbar = false;
  this.trayIcon.Visible = true;
	}
}

And a double-click on the trayIcon restores it by setting this.WindowState to FormWindowState.Normal. The code to ensure the single-instance currently looks like this:

static void Main() {
	Process[] RunningProcesses = Process.GetProcessesByName("MyApp");
	if(RunningProcesses.Length == 1) {
  Application.Run(new Form1());
	}
	else {
  ShowWindowAsync(RunningProcesses[0].MainWindowHandle,SW_RESTORE);
  SetForegroundWindow(RunningProcesses[0].MainWindowHandle);
	}
}

Any ideas?

Link to comment
https://www.neowin.net/forum/topic/336677-c-restoring-an-app-from-the-systray/
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.