Click here to Skip to main content
13,090,426 members (36,143 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.1K views
4 bookmarked
Posted 2 Mar 2010

Allow only one instance of a c# application

, 2 Mar 2010
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
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 3 Mar 2010
Article Copyright 2010 by Rudy du Plooy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid