Click here to Skip to main content
Click here to Skip to main content

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.
 
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

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
Questionwhere to put this code... Pinmembernarenderkumardhull31-May-13 19:51 
GeneralReason for my vote of 2 Sorry for bad vote, nice video, nice... Pinmemberjohannesnestler24-Jan-12 6:03 
GeneralReason for my vote of 5 Good work,thx PinmemberPranit Kothari26-Dec-11 14:42 
GeneralReason for My Vote of 5 Nice video! once again great work by... PinmvpRaviRanjankr3-Nov-11 19:35 
GeneralReason for my vote of 4 Very useful when you need it PinmemberMember 78028731-Nov-11 12:11 
GeneralReason for my vote of 4 Might be good to add some more infor... PinmemberBrianBissell1-Nov-11 3:04 
GeneralReason for my vote of 5 awesome video PinmemberSanjeevSingh31-Oct-11 19:39 
Suggestionare you sure about the > 1 checking Pinmemberdavidkz231-Nov-11 2:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140821.2 | Last Updated 24 Oct 2011
Article Copyright 2011 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid