Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Run Only One Copy Of Application

, 25 Mar 2011
Rate this:
Please Sign up or sign in to vote.
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] ...

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

Comments and Discussions

 
GeneralReason for my vote of 5 The SetForgroundWindow is a nice fea... PinmemberRoy from Detroit12-Dec-11 4:52 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 25 Mar 2011
Article Copyright 2011 by FDW
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid