Click here to Skip to main content
13,147,376 members (27,070 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


24 bookmarked
Posted 21 Oct 2011

Video tip C#: Ensure only one instance is running

, 24 Oct 2011
Rate this:
Please Sign up or sign in to vote.
How to ensure that only 1 instance of the windows application is running
In this trick, we will try to understand how we can ensure that only one instance of a C# program is running on a computer. I have also created a simple video for the trick below. So you can either see the video or read the below textual description.[^]

First, import the system.diagnostic namespace.

using System.Diagnostics;

Second, use the process object to get the process name.

// we need to get the current process name
string strProcessName = Process.GetCurrentProcess().ProcessName;

Find the process name in the current process collection.

// check if this process name is existing in the current running processes
Process[] Oprocesses = Process.GetProcessesByName(strProcessName);

Depending on the process count, make your decision.

// if its existing then exit
if (Oprocesses.Length > 1)
MessageBox.Show("The application is already running");
// else let the below code run
Application.Run(new Form1());


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

QuestionGreat.......... Pin
jithesh a10-Nov-14 21:46
memberjithesh a10-Nov-14 21:46 
BugVideo is not available Pin
Gaurav.Kumar.Arora27-Oct-14 9:37
memberGaurav.Kumar.Arora27-Oct-14 9:37 
Questionwhere to put this code... Pin
narenderkumardhull31-May-13 19:51
membernarenderkumardhull31-May-13 19:51 
GeneralReason for my vote of 2 Sorry for bad vote, nice video, nice... Pin
johannesnestler24-Jan-12 6:03
memberjohannesnestler24-Jan-12 6:03 
GeneralReason for my vote of 5 Good work,thx Pin
Pranit Kothari26-Dec-11 14:42
memberPranit Kothari26-Dec-11 14:42 
GeneralReason for My Vote of 5 Nice video! once again great work by... Pin
RaviRanjankr3-Nov-11 19:35
mvpRaviRanjankr3-Nov-11 19:35 
GeneralReason for my vote of 4 Very useful when you need it Pin
Member 78028731-Nov-11 12:11
memberMember 78028731-Nov-11 12:11 
GeneralReason for my vote of 4 Might be good to add some more infor... Pin
BrianBissell1-Nov-11 3:04
memberBrianBissell1-Nov-11 3:04 
GeneralReason for my vote of 5 awesome video Pin
SanjeevSingh31-Oct-11 19:39
memberSanjeevSingh31-Oct-11 19:39 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 24 Oct 2011
Article Copyright 2011 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid