What I've learned today:
The final application startup tweak we'll discuss is preventing a second instance of the application from starting. Of course, some document-based applications make sense to run multiple times, but many utilities make no sense, or can even cause problems when run this way. It turns out that we can easily make a change to accomplish this. In Visual Basic, you can simply click the Make single instance application checkbox in Project Settings to enable single-instance mode. In C# (and VB for that matter), you can create the same effect in the Main method.
using System;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
bool exclusive;
Mutex m = new Mutex(true, "C4F-TrickedOut", out exclusive);
if( exclusive )
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 mainForm = new Form1();
Application.Run();
}
else
{
MessageBox.Show("Another instance is already running.");
}
}
}
Very nice.
More like this here (Coding4Fun)