Plan B: Use a little code from VB.
Be sure to add a reference to Microsoft Visual Basic then add this code to your Program.CS file
- /// <summary>
-
/// The main entry point for the application.
-
/// </summary>
-
[STAThread]
-
static void Main(string[] commandLine)
-
{
-
Control.CheckForIllegalCrossThreadCalls = false;
-
if (bSplash) SplashScreen.ShowSplashScreen();
-
if (bSplash) SplashScreen.SetStatus("Getting Environmental information");
-
-
Application.EnableVisualStyles();
-
App myApp = new App();
-
myApp.Run(commandLine);
-
}
-
}
-
-
-
-
/// <summary>
-
/// We inherit from WindowsFormApplicationBase which contains the logic for the application model, including
-
/// the single-instance functionality.
-
/// </summary>
-
class App : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
-
{
-
public App()
-
{
-
this.IsSingleInstance = true; // makes this a single-instance app
-
this.EnableVisualStyles = true; // C# windowsForms apps typically turn this on. We'll do the same thing here.
-
this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses; // the vb app model supports two different shutdown styles. We'll use this one for the sample.
-
}
-
-
/// <summary>
-
/// This is how the application model learns what the main form is
-
/// </summary>
-
protected override void OnCreateMainForm()
-
{
-
this.MainForm = new Form1();
-
}
-
-
-
/// <summary>
-
/// Gets called when subsequent application launches occur. The subsequent app launch will result in this function getting called
-
/// and then the subsequent instances will just exit. You might use this method to open the requested doc, or whatever
-
/// </summary>
-
/// <param name="eventArgs"></param>
-
protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
-
{
-
base.OnStartupNextInstance(eventArgs);
-
//System.Windows.Forms.MessageBox.Show("An attempt to launch another instance of this app was made");
-
}
-
-
}
-
You probably want to delete or comment out lines 8 and 9 since they are for a splash screen that won't exist in your project.