Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Run Only One Copy Of Application

0.00/5 (No votes)
30 Mar 2011 1  
Just because we like C# doesn't mean we can't borrow from other .NET languages. I've always had good luck and less complication using VB's single instance methodology in my C# applications. This code would go in your program.cs file:/// /// We inherit from...
Just because we like C# doesn't mean we can't borrow from other .NET languages. I've always had good luck and less complication using VB's single instance methodology in my C# applications. This code would go in your program.cs file:
C#
/// <summary>
///  We inherit from WindowsFormApplicationBase which contains
///  the logic for the application model, including
///  the single-instance functionality.
/// </summary>
public class App : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
    #region Constructors

    public App()
    {
        this.IsSingleInstance = true; // makes this a single-instance app
        // C# windowsForms apps typically turn this on.  We'll do the same thing here.
        this.EnableVisualStyles = true;
        // the vb app model supports two different shutdown styles.
        // We'll use this one for the sample.
        this.ShutdownStyle = 
         Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses;
    }

    #endregion Constructors

    #region Methods

    /// <summary>
    /// This is how the application model learns what the main form is
    /// </summary>
    protected override void OnCreateMainForm()
    {
        this.MainForm = Program.MainForm;
    }

    /// <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");
    }

    #endregion Methods
}

public static class Program
{
    #region Fields
    public static string[] CommandLine;
    public static Form1 MainForm = new Form1();
    public static App myApp = new App();
    #endregion Fields

    #region Events
    #endregion Events

    #region Properties
    #endregion Properties

    #region Methods


    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThreadAttribute]
    static void Main(string[] commandLine)
    {
        // The one and only "Main" method of an application.
        // This is what runs when you launch the application
        // Everything else is a result of what happens here

        CommandLine = commandLine;
        Control.CheckForIllegalCrossThreadCalls = false;
        Application.EnableVisualStyles();

        // Do whatever preparation you like... load settings... whatever

        // Now that we *finally* have everything set up, let's run one instance of our App
        try
        {
            string cmdls = string.Empty;
            foreach (var s in commandLine)
            {
                cmdls += s + "\n";
            }
            myApp.Run(commandLine);
        }
        catch (Exception err)
        {
            Console.WriteLine(err.Message, "Main");
        }
    }

    #endregion Methods
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here