Click here to Skip to main content
Click here to Skip to main content

Run Only One Copy Of Application

By , 25 Mar 2011
 

Maybe this is helpful, it tries to switch to the first running instance:

namespace UltraSimple.Win
{
    static class Program
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
 
        [STAThread]
        static void Main()
        {
            Process oProcess = Process.GetCurrentProcess();
            Process[] aoProcesses = Process.GetProcessesByName(oProcess.ProcessName);
            if (aoProcesses.Length > 1)
            {
                MessageBox.Show(
                      "The application \""
                    + oProcess.ProcessName
                    + "\" is already running.", "Ultrasimple");
                Program.SetForegroundWindow(aoProcesses[aoProcesses[0].Id == 
                        oProcess.Id ? 1 : 0].MainWindowHandle);
                return;
            }
            ...
        }
    }
}

License

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

About the Author

FDW

Netherlands Netherlands
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 The SetForgroundWindow is a nice fea...memberRoy from Detroit12-Dec-11 4:52 
GeneralHave not done a perf mon on both of the solutions, but findi...memberzenwalker198516-Oct-11 4:05 
Have not done a perf mon on both of the solutions, but finding a process name is more costly than mutex way, i think.
GeneralReason for my vote of 5 nice alternativemembercharles henington22-Apr-11 6:07 
GeneralReason for my vote of 5 Reason for my vote of 5 Nice way to...memberRK_DBA14-Apr-11 17:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 25 Mar 2011
Article Copyright 2011 by FDW
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid