ok, the title is pretty long and should tell the problem i’m facing with.
Here is the code when minimizing to icon tray:
void MainFormResize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
this.ShowInTaskbar = false;
}
}
When the program is already opened and in sys tray, and still someone wants to open another instance of it, then:
private static void Main(string[] args)
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "IPADcommunicator", out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
IntPtr handle = FindWindow(null,"IPADcommunicator");
SetForegroundWindow(handle);
ShowWindow(handle,5);
break;
}
}
...
Howeve, it is not working properly. The mainwindow is not restored.
I’ve googled a lot and haven’t found solutions for that problem.
Thanks in advance!
Calling SetForegroundWindow() on an invisible window isn’t going to work. There are many other possible failure mode, FindWindow() is a miserable one when you start passing null.
Don’t invent this yourself, .NET already has great built-in support for single instance apps. You can even get a notification when a 2nd copy starts and pass the command line. Which is what you want here, simply restore the window instead of hacking the API. The code you need is here.