Tuesday, July 22, 2008

Windows From ----- Only one instance of the program can be started

Don't want user to start your application twice ? Use the code below:

static void Main()
{
bool instanceCountOne = false;

using (Mutex mtex = new Mutex(true, "MYApplication", out instanceCountOne))
{
if (instanceCountOne)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

using(login l = new login())
{
if (l.ShowDialog() == DialogResult.OK)
{
l.Dispose();

if (Properties.Settings.Default.from_database == true && Properties.Settings.Default.Start_CommitChanges == true)
{
Connecter c = new Connecter();
commit_changes(c);
}

Application.Run(new GUI());
}
else
{
l.Dispose();
//MessageBox.Show("Login Cancelled.");
}
}

mtex.ReleaseMutex();
}
else
{
MessageBox.Show("An application instance is already running");
}
}
}