Click here to Skip to main content
11,647,828 members (58,648 online)
Click here to Skip to main content

Tagged as

Run Only One Copy Of Application

, 25 Mar 2011 CPOL 3.8K 4
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)

Share

About the Author

FDW
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
raj ch6-Jul-13 0:11
memberraj ch6-Jul-13 0:11 
GeneralReason for my vote of 5 The SetForgroundWindow is a nice fea... Pin
Roy from Detroit12-Dec-11 4:52
memberRoy from Detroit12-Dec-11 4:52 
Reason for my vote of 5
The SetForgroundWindow is a nice feature, it tries to help solve the original problem (that the user apparently lost the window), instead of simply stopping the user and increasing frustration.
GeneralHave not done a perf mon on both of the solutions, but findi... Pin
zenwalker198516-Oct-11 4:05
memberzenwalker198516-Oct-11 4:05 
GeneralReason for my vote of 5 nice alternative Pin
charles henington22-Apr-11 6:07
membercharles henington22-Apr-11 6:07 
GeneralReason for my vote of 5 Reason for my vote of 5 Nice way to... Pin
RK_DBA14-Apr-11 17:44
memberRK_DBA14-Apr-11 17:44 

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 | Terms of Use | Mobile
Web04 | 2.8.150804.4 | Last Updated 25 Mar 2011
Article Copyright 2011 by FDW
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid