Click here to Skip to main content
11,922,416 members (30,799 online)
Click here to Skip to main content
Add your own
alternative version


105 bookmarked

Detect if another process is running and bring it to the foreground

, 30 Sep 2002
Rate this:
Please Sign up or sign in to vote.
Sometimes, you only want one instance of your application running. This is a C# implementation that tests to see if an instance of your application is already running, and brings it to the foreground if it is.


The following code demonstrates how to detect if there is an instance of your application already running. If detected, it will bring that application to the foreground (restoring its window state if iconic), and then terminating the current application. This is useful in instances where you want to ensure that only one instance of your application is running.

This code was put together using the prior work of Taylor Wood ("Window Hiding in C#") and a message in the C# forum posted by David Wengier illustrating the use of the Process class. Without their help, this would have taken me several hours to put together, instead of a few minutes!

The Code

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class AppMain
    [DllImport("user32.dll")] private static extern 
        bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")] private static extern 
        bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")] private static extern 
        bool IsIconic(IntPtr hWnd);

    private const int SW_HIDE = 0;
    private const int SW_SHOWNORMAL = 1;
    private const int SW_SHOWMINIMIZED = 2;
    private const int SW_SHOWMAXIMIZED = 3;
    private const int SW_SHOWNOACTIVATE = 4;
    private const int SW_RESTORE = 9;
    private const int SW_SHOWDEFAULT = 10;

    static void Main()
        // get the name of our process
        string proc=Process.GetCurrentProcess().ProcessName;
        // get the list of all processes by that name
        Process[] processes=Process.GetProcessesByName(proc);
        // if there is more than one process...
        if (processes.Length > 1)
            // Assume there is our process, which we will terminate, 
            // and the other process, which we want to bring to the 
            // foreground. This assumes there are only two processes 
            // in the processes array, and we need to find out which 
            // one is NOT us.

            // get our process
            Process p=Process.GetCurrentProcess();
            int n=0;        // assume the other process is at index 0
            // if this process id is OUR process ID...
            if (processes[0].Id==p.Id)
                // then the other process is at index 1
            // get the window handle
            IntPtr hWnd=processes[n].MainWindowHandle;
            // if iconic, we need to restore the window
            if (IsIconic(hWnd))
                ShowWindowAsync(hWnd, SW_RESTORE);
            // bring it to the foreground
            // exit our process
        // ... continue with your application initialization here.


This code is a great example of interfacing with Win32 libraries and using some esoteric functions in .NET.


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


About the Author

Marc Clifton
United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website,, where you will find many of his articles and his blog.

Marc lives in Philmont, NY.

You may also be interested in...

Comments and Discussions

Suggestionworkaround for when process cannot bring other window to foreground Pin
theory200619-Jul-12 1:16
membertheory200619-Jul-12 1:16 
GeneralMy vote of 4 Pin
theory200619-Jul-12 1:13
membertheory200619-Jul-12 1:13 
QuestionUse this article In Windows CE 5.0 Pin
Tyler4510-Jan-07 9:47
memberTyler4510-Jan-07 9:47 
QuestionHow to get it work with Compact Framework ? Pin
Mikael Braad Nielsen26-Jan-06 2:28
memberMikael Braad Nielsen26-Jan-06 2:28 
AnswerRe: How to get it work with Compact Framework ? Pin
Gizz15-Nov-06 0:59
memberGizz15-Nov-06 0:59 
AnswerRe: How to get it work with Compact Framework ? Pin
siSidon23-Apr-07 6:10
membersiSidon23-Apr-07 6:10 
QuestionWhat about notification area?? Pin
Raananan28-Nov-05 9:24
memberRaananan28-Nov-05 9:24 
AnswerRe: What about notification area?? Pin
kubben13-Dec-05 4:05
memberkubben13-Dec-05 4:05 
QuestionNice, but Pin
Chazsoft7-Oct-05 4:18
memberChazsoft7-Oct-05 4:18 
QuestionWhat if? Pin
Super Lloyd27-Jun-05 2:19
memberSuper Lloyd27-Jun-05 2:19 
QuestionApp name changed? Pin
TNTVN6-Dec-04 20:18
memberTNTVN6-Dec-04 20:18 
GeneralGreat article, almost everything I need Pin
snappa1-Mar-04 12:58
membersnappa1-Mar-04 12:58 
QuestionIs there a 100% NET way to do this? Pin
m.chung14-Feb-04 4:01
memberm.chung14-Feb-04 4:01 
AnswerRe: Is there a 100% NET way to do this? Pin
Marc Clifton15-Feb-04 5:39
editorMarc Clifton15-Feb-04 5:39 
GeneralRe: Is there a 100% NET way to do this? Pin
ykpui31-Aug-15 19:31
memberykpui31-Aug-15 19:31 
GeneralRe: Is there a 100% NET way to do this? Pin
ykpui31-Aug-15 19:31
memberykpui31-Aug-15 19:31 
AnswerRe: Is there a 100% NET way to do this? Pin
ykpui31-Aug-15 18:27
memberykpui31-Aug-15 18:27 
GeneralAnother approach: using mutexes Pin
Guido_d20-Jan-04 3:58
memberGuido_d20-Jan-04 3:58 
GeneralRe: Another approach: using mutexes Pin
Marc Clifton20-Jan-04 4:07
editorMarc Clifton20-Jan-04 4:07 
GeneralHiding problem Pin
Neoshid21-Oct-03 1:02
memberNeoshid21-Oct-03 1:02 
GeneralRe: Hiding problem Pin
C# newuser23-Apr-05 13:59
sussC# newuser23-Apr-05 13:59 
GeneralMore than 15 characters ProcessName Pin
Z42Cool17-Jul-03 0:19
memberZ42Cool17-Jul-03 0:19 
GeneralRe: More than 15 characters ProcessName Pin
Anonymous20-Aug-03 10:12
sussAnonymous20-Aug-03 10:12 
GeneralCool Pin
Kant6-Apr-03 17:17
memberKant6-Apr-03 17:17 
GeneralRestore the application from notify icon (system tray) Pin
Qui17-Oct-02 3:19
sussQui17-Oct-02 3:19 

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.151120.1 | Last Updated 1 Oct 2002
Article Copyright 2002 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid