Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

Run Only One Copy Of Application

, 30 Mar 2011
Rate this:
Please Sign up or sign in to vote.
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:
/// <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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

tlhintoq
Software Developer http://web.me.com/tlhintoq/Clints_World/Welcome.ht
United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 2 Most usefull for me PinmemberMoahmedkaz9-May-11 19:00 
GeneralReason for my vote of 4 nice not a fan of vb but a 4 none th... Pinmembercharles henington22-Apr-11 6:08 
Generalhmmm - I don't like Control.CheckForIllegalCrossThreadCalls ... Pinmemberjohannesnestler3-Apr-11 10:26 
GeneralRe: It really doesn't make a differnce the Control.CheckForIlleg... Pinmembercharles henington19-Apr-11 11:38 
GeneralThe main problem with this approach is that it uses Remoting... PinmemberPhil J Pearson2-Apr-11 10:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 30 Mar 2011
Article Copyright 2011 by tlhintoq
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid