Click here to Skip to main content
12,349,169 members (57,104 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

45K views
23 bookmarked
Posted

Video tip C#: Ensure only one instance is running

, 24 Oct 2011 CPOL
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.

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

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
{
// else let the below code run
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

License

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

Share

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 
SuggestionMessage Removed Pin
davidkz231-Nov-11 2:06
memberdavidkz231-Nov-11 2:06 

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.160621.1 | Last Updated 24 Oct 2011
Article Copyright 2011 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid