Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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);
  // MainClientForm vcf = (MainClientForm)process.Handle;
  // vcf.BringToFront();
                    IntPtr hWnd = process.MainWindowHandle;
                    if (IsIconic(hWnd) != 0)  // Doesn't go into this
                    {
                        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.
Posted
Updated 16-Feb-11 23:11pm
v4
Comments
JF2015 17-Feb-11 3:17am    
Edited to fix the links.
All Time Programming 17-Feb-11 3:47am    
Thanks JF2015.

google:

c# maximize another application

And yes, I read your post, but "another" is correct!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Feb-11 3:47am    
That should be enough, right answer, my 5.
--SA
All Time Programming 17-Feb-11 3:48am    
I didn't get what you are trying to say !!
JF2015 17-Feb-11 3:55am    
Correct answer that's helpfull. If not, the OP needs to show his code. 5+
All Time Programming 17-Feb-11 4:04am    
Friends, have already added my code in the question. I hope that will help you to know my problem much better. Thanks.
Thanks to all,

Found the solution at :
<a href="http://www.codeproject.com/KB/cs/SingleInstanceAppMutex.aspx">C# Single Instance App With the Ability To Restore From System Tray (Using Mutex)</a>[<a href="http://www.codeproject.com/KB/cs/SingleInstanceAppMutex.aspx" target="_blank" title="New Window">^</a>]

After trying with source code of 3 sites and debugging, this site helped me achieve what I wanted.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900