11

I have developed a C# Windows application & created a exe of it.

What I want is that when ever I try to run the application, if it is already in running state, then activate that application instance, else start new application.

That means I don't want to open same application more than one time

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ayush
  • 165
  • 1
  • 3
  • 10
  • Please check following you might get what you are looking for http://stackoverflow.com/questions/2197620/how-to-set-focus-and-launch-the-already-running-application-on-button-click-event – Amritpal Singh Sep 09 '11 at 06:01
  • Pure and clean: .NET version without DLL hell. https://stackoverflow.com/questions/5990118/maximize-window-from-the-main-function/32322918#32322918 – Christian Apr 10 '18 at 06:35

4 Answers4

16

Use the following code to set focus to the current application:

        [DllImport("user32.dll")]
        internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        ... 
        Process currentProcess = Process.GetCurrentProcess();
        IntPtr hWnd = currentProcess.MainWindowHandle;
        if (hWnd != IntPtr.Zero)
        {
            SetForegroundWindow(hWnd);
            ShowWindow(hWnd, User32.SW_MAXIMIZE);
        }
Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
  • 1
    +1 for the order or commands (first SetForeground.. and then ShowWindow) otherwise it just doesn't work! – Uri Abramson Jun 11 '13 at 14:28
  • 6
    In case you don't have the User32 enum, the values can be found at: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx – BradleyDotNET Feb 28 '14 at 23:42
  • Enum for copy/paste can be found here: http://www.pinvoke.net/default.aspx/Enums/SHOWWINDOW_FLAGS.html . Also, the ShowWindow should not be necessary. – Andreas Reiff Oct 08 '15 at 06:57
  • 1
    using System.Diagnostics; using System.Runtime.InteropServices; – timothy Apr 04 '18 at 07:52
6

You can PInvoke SetForegroundWindow() and SetFocus() from user32.dll to do this.

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

// SetFocus will just focus the keyboard on your application, but not bring your process to front.
// You don't need it here, SetForegroundWindow does the same.
// Just for documentation.
[DllImport("user32.dll")]
static extern IntPtr SetFocus(HandleRef hWnd);

As argument you pass the window handle of the process you want to bring in the front and focus.

SetForegroundWindow(myProcess.MainWindowHandle);

SetFocus(new HandleRef(null, myProcess.Handle)); // not needed

Also see the restrictions of the SetForegroundWindow Methode on msdna.

Felix C
  • 1,755
  • 5
  • 26
  • 40
1

Use the following code part for multiple Instance checking of an exe, and if its true return at form load. For running this functionality in your app include using System.Diagnostics; namespace

private bool CheckMultipleInstanceofApp()
        {
            bool check = false;
            Process[] prc = null;
            string ModName, ProcName;
            ModName = Process.GetCurrentProcess().MainModule.ModuleName;
            ProcName = System.IO.Path.GetFileNameWithoutExtension(ModName);
            prc = Process.GetProcessesByName(ProcName);
            if (prc.Length > 1)
            {
                MessageBox.Show("There is an Instance of this Application running");
                check = true;
                System.Environment.Exit(0);
            }
            return check;
        }
1

Use Mutex to launch single instance of the application. Also, you can use Process class to find your application and SetFocus on it. Here http://social.msdn.microsoft.com/Forums/da-DK/csharpgeneral/thread/7fd8e358-9709-47f2-9aeb-6c35c7521dc3

Zenwalker
  • 1,883
  • 1
  • 14
  • 27