Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I check if my application is already running? 1

Status
Not open for further replies.

ddvlad

Programmer
Mar 12, 2003
71
RO
I don't want users to be able to run an application more than once at the same time. How can I check if the app is already running?
Thanks in advance.

Life is a bad joke -- don't take it serious...
 
Use a named mutex
Code:
System.Threading.Mutex

-pete
 
Hi !

Code:
if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 0)
{
	this.Close();
}
 
Thanks a lot to both of you!

Life is a bad joke -- don't take it serious...
 
So if you put that code in the OnLoad() override of the main Form the first instance will close. If you change the test to be greater than 1 it will work. However if a user copies the .exe file to a different name then they can create an instance for each copy of the file they create.

Using a Mutex eliminates both of those limitations.


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top