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

Tagged as

Allow only one instance of a c# application

, 2 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This code creates a mutex before starting an application and releases the mutex when the application is closed. A user will only be able to start the application once. This can be used for services as well./// /// The main entry point for the application.///...
This code creates a mutex before starting an application and releases the mutex when the application is closed. A user will only be able to start the application once. This can be used for services as well.
 
/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]
 
public static void Main()
 
{
 
bool createdMutex = true;

Mutex mutex = new Mutex(true, "ConfigureApp", out createdMutex);

if (createdMutex && mutex.WaitOne())
 
{
 
Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new MainForm());

mutex.ReleaseMutex();

}
 
else
 
{
 
MessageBox.Show("Only one application instance is allowed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

}
 
mutex.Close();

}

License

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

Share

About the Author

Rudy du Plooy
Team Leader
South Africa South Africa
Studied Engineering Computer Science at Techinikon Pretoria, South Africa.
Have been a Software Developer for 9 year using different technologies nl. Assembler 8, 16, 32 and 64 bit, Delphi 6 and 7, C++, Java, ASP. For last 7 year I focused on C# and ASP.net also have extensive SQL 2000, 2005 and 2008 experience. I am currently employed as a Software Development Team leader and Software Architect at a vehicle tracking company in South Africa.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 3 Mar 2010
Article Copyright 2010 by Rudy du Plooy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid