Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Run Only One Copy Of Application

0.00/5 (No votes)
25 Mar 2011 1  
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:


C#
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here