Click here to Skip to main content
15,881,866 members
Articles / Programming Languages / C#
Tip/Trick

Video tip C#: Ensure only one instance is running

Rate me:
Please Sign up or sign in to vote.
4.65/5 (12 votes)
24 Oct 2011CPOL 63.6K   24   9
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.

http://www.youtube.com/watch?v=8pjfPNfQP2Q[^]

First, import the system.diagnostic namespace.
 

C#
C#
using System.Diagnostics;


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

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


Find the process name in the current process collection.

C#
C#
// 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.

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

For Further reading do watch  the below interview preparation videos and step by step video series.

License

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


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
QuestionGreat.......... Pin
jithesh a10-Nov-14 21:46
jithesh a10-Nov-14 21:46 
BugVideo is not available Pin
Gaurav Aroraa27-Oct-14 9:37
professionalGaurav Aroraa27-Oct-14 9:37 
Questionwhere to put this code... Pin
_Dhull 31-May-13 19:51
_Dhull 31-May-13 19:51 
GeneralReason for my vote of 2 Sorry for bad vote, nice video, nice... Pin
johannesnestler24-Jan-12 6:03
johannesnestler24-Jan-12 6:03 
GeneralReason for my vote of 5 Good work,thx Pin
Pranit Kothari26-Dec-11 14:42
Pranit Kothari26-Dec-11 14:42 
GeneralReason for My Vote of 5 Nice video! once again great work by... Pin
RaviRanjanKr3-Nov-11 19:35
professionalRaviRanjanKr3-Nov-11 19:35 
GeneralReason for my vote of 4 Very useful when you need it Pin
Just Russell1-Nov-11 12:11
professionalJust Russell1-Nov-11 12:11 
GeneralReason for my vote of 4 Might be good to add some more infor... Pin
BrianBissell1-Nov-11 3:04
BrianBissell1-Nov-11 3:04 
GeneralReason for my vote of 5 awesome video Pin
SanjeevSingh31-Oct-11 19:39
SanjeevSingh31-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.