Click here to Skip to main content
12,350,597 members (24,732 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.1K views
5 bookmarked
Posted

Run Only One Copy Of Application

, 25 Mar 2011 CPOL
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 
GeneralHave not done a perf mon on both of the solutions, but findi... Pin
zenwalker198516-Oct-11 4:05
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 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    Praise Praise    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
Web02 | 2.8.160621.1 | Last Updated 25 Mar 2011
Article Copyright 2011 by FDW
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid