Single instance application with C#

If you’re developing a winforms application with VB.NET you can set a checkbox in the designer to have a single instance application. This option is not available to C# developers but since language does not matter in the .NET space you can just use the same classes the VB.NET designer uses.

By subclassing WindowsFormsApplicationBase you can get the same behaviour. The class can be found in the Microsoft.VisualBasic.ApplicationServices namespace.

public class SingleInstanceManager
      : WindowsFormsApplicationBase
{
 
    public SingleInstanceManager()
    {
        IsSingleInstance = true;
        EnableVisualStyles = true;
        ShutdownStyle = ShutdownMode.AfterMainFormCloses;
    }
 
    protected override bool OnStartup(StartupEventArgs e)
    {
        return true;
    }
 
    protected override void OnCreateMainForm()
    {
        base.OnCreateMainForm();
        MainForm = new MainForm();
        HideSplashScreen();
    }
 
    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
    {
        base.OnStartupNextInstance(eventArgs);
        MainForm.Focus();
    }
 
    protected override void OnCreateSplashScreen()
    {
        base.OnCreateSplashScreen();
        SplashScreen = new SplashForm();
    }
}

Change your Program.cs to use this new class.

[STAThread]
static void Main()
{
    var manager = new SingleInstanceManager();
    manager.Run(new string[]{});
}

Tags:

65 Responses to “Single instance application with C#”

  1. FRED says:

    terry abbot

    Buy_generic drugs…

  2. ARTURO says:

    macon georgia drug court

    Buy_without prescription…

  3. JOHN says:

    canadian@pharmacy.valtrex” rel=”nofollow”>…

    Buygeneric meds…

  4. BRETT says:

    indications@for.use.of.atrovent” rel=”nofollow”>…

    Buygeneric meds…

  5. marilyn harrison says:

    long hairstyles

    I think this is one of the most significant information for me. And i am glad reading your article. But wanna remark on some general things, The site style is great, the articles is really nice : D. Good job, cheers…

  6. JOHN says:


    Buy Viagra

    Buy Quality Drugs Now!…

  7. ARNOLD says:


    Buy Lotemax Online

    Check Good Meds Now!…

  8. CHRISTIAN says:


    Buy@Arcoxia.Online” rel=”nofollow”>..

    Purchase Quality Pharmacy Today!…

  9. DENNIS says:


    Buy@Arcoxia.Online” rel=”nofollow”>.

    Purchase Cheap Meds Now!…

  10. CARLTON says:


    Buy Lotemax Online

    Buy Unique Generic.Drugs Today!…

Leave a Reply