Click here to Skip to main content
15,886,071 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have create an application with 2 form : form1 and form2
we can open the application only one time if the application is already running on form1 and i press on the .exe file if focus on the current form1 already opened the problem is when the form2 is opened and the form1 is hide when i press on the application .exe file it does not focus on the form2 it focus on the hiden form1 how can i make it focus and activate form2 if the form1 is hide();

Mutex mutex = new Mutex(false, "UpdateExcel");
            
            //Instance already running
            if (!mutex.WaitOne(0, false))
            {
                IntPtr winHandle = IntPtr.Zero;
                Process[] localAll = Process.GetProcesses();
                Process me = Process.GetCurrentProcess();

                foreach (Process p in localAll)
                {
                   
                    if (p.Id != me.Id) 
                    {
                            if(p.ProcessName.Equals(me.ProcessName))
                            {
                                winHandle = Process.GetProcessById((int)p.Id).MainWindowHandle;
                            break;
                            }
                    }
                }

                if (winHandle != IntPtr.Zero)
                {
                    const int SW_RESTORE = 9;
                    if (IsIconic(winHandle) != 0) ShowWindow(winHandle, SW_RESTORE);
                    SetForegroundWindow(winHandle);
               
                }
                mutex.Close();
                System.Environment.Exit(0);
            }
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
           Application.Run(new Form1());
Posted
Comments
Sergey Alexandrovich Kryukov 30-Nov-13 18:55pm    
So, .NET 1.0 or 4.0? V.1.0, practically, does not exist, the first "real" .NET version was, perhaps, v.2.0...
—SA
Sergey Alexandrovich Kryukov 30-Nov-13 19:01pm    
What are you trying to achieve? Is it about having some application in only one instance, preventing second instance to run?
How can it possibly related to your forms? It all does not seem to make much sense...
—SA
ZurdoDev 30-Nov-13 21:02pm    
What?

1 solution

An interesting question, and I hope a mutex guru will be along shortly ... meanwhile ...

I assume this is Windows Forms.

If you are willing to reference the VisualBasic.dll in your C# project, a non-mutex solution is available. This would use the Microsoft.VisualBasic.ApplicationServices library in the VB.NET facilities, and, specifically, use the WindowsFormsApplicationBase Class.

Chris Sells explored this in Chapter 14 of his book, "Windows Forms 2.0 Programming," and, that chapter, "Applications," ... with updated content since publication ... is available, free, for download, as a .pdf, here: [^].

The content on Singleton Applications begins at pp. 560, and he includes specific details, and code, for accomplishing what ... I think ... you want.

The source-code for the 2nd. edition of the book is downloadable as a .zip file here: [^], and in the source-code for Chapter 14 is an example of a SingleInstanceApplication. I've verified that you can convert that code sample to VS 2012 and run it, compiling against either FrameWork 2.0, or 4.5.

Trying out the scenario where the application is run once, and the MainForm is hidden and a second Form is shown, and then double-clicking the Application 'exe file: the first Form is not shown automatically, but, the second Form does not get focus.

If I have time, later today, I'll see if I can modify Sells' code to give the second Form focus if the first Form is hidden and the user tries to run the Application a second time.

Disclaimer: I was a technical editor for Addison-Wesley on several chapters of Sells' book, but not Chapter 14.
 
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