Click here to Skip to main content
12,396,606 members (63,394 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.2K 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

 
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 

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.160721.1 | Last Updated 25 Mar 2011
Article Copyright 2011 by FDW
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid