Hello,
I want to run only a single instance of my application and restore it if user tries to execute another. I have written code using
http://iridescence.no/post/CreatingaSingleInstanceApplicationinC.aspx[
^] and
Single Instance Application in C#[
^]
but none of them show the minimized or already running application. Actually the application stays in system tray once its minimized. Is their any way to show the application from system tray. Can we find it like IsIconic (stating that its minimized) ?
In Program.cs, I am callign my main form, so main is in Program.cs and not my main form.
At present I just show a message box stating that the application is running. But I want to restore it (if its in system tray ) or bring to front if its hidden by other windows. Currently have implemetned as shown in iridescence.no.... site.
[EDIT:]
This is the code, I am implementing :
static void Main()
{
const int SW_RESTORE = 9;
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "Ultimate VPN", out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainClientForm());
GC.KeepAlive(mutex);
}
else
{
Process current = Process.GetCurrentProcess();
Console.WriteLine"OK PROCESS IS ALREADY RUNNING - nAME = " + current.ProcessName + " ID = " + current.Id);
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id == current.Id)
{
Console.WriteLine"Found SAMe ID - process Id = " + process.Id);
IntPtr hWnd = process.MainWindowHandle;
if (IsIconic(hWnd) != 0)
{
ShowWindow(hWnd, SW_RESTORE);
}
bool showed = SetForegroundWindow(hWnd);
Console.WriteLine"Have set foreground Window SHOWED = " + showed);
MessageBox.Show("MainClientForm is already running in System Tray");
break;
}
}
}
}
}
I have handled IsIconic, but that doesn't work as I guess the application is either in system tray or hidden. How do I find from the system tray ?
Any help is highly appreciated.
Thanks
UPDATE:
OP resolved the issue by himself - Posted the solution as one of the answers.