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:

60 Responses to “Single instance application with C#”

  1. Alexander6 says:

    buy@generic.VIAGRA” rel=”nofollow”>…

    Need cheap generic VIAGRA?…

  2. Alexander7 says:

    buy@generic.LEVITRA” rel=”nofollow”>…

    Need cheap generic LEVITRA?…

  3. LEWIS says:

    Order@Cheap.Abana” rel=”nofollow”>..

    Buynow it…

  4. SALVADOR says:

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

    Buygeneric meds…

  5. RONNIE says:

    Buy@Cheap.Abilify” rel=”nofollow”>..

    Buynow it…

  6. RUSSELL says:

    Order@Generic.Acai” rel=”nofollow”>..

    Buywithout prescription…

  7. PERRY says:

    Purchase@Cheap.Abilify” rel=”nofollow”>…

    Buywithout prescription…

  8. TERRENCE says:

    Get@Abilify.Online” rel=”nofollow”>..

    Buyit now…

  9. GUY says:

    Buy@Cheap.Acai” rel=”nofollow”>.

    Buywithout prescription…

  10. JEFFREY says:

    Order@Acai.Online” rel=”nofollow”>…

    Buynow it…

  11. DONALD says:

    Order@Acai.Online” rel=”nofollow”>…

    Buyit now…

  12. WILLIE says:

    Purchase@Discount.Acai” rel=”nofollow”>…

    Buygeneric drugs…

  13. PERRY says:

    Cheap@Acai.Online” rel=”nofollow”>…

    Buynow…

  14. SAM says:

    Purchase@Discount.Energy.Boost” rel=”nofollow”>.…

    Buynow it…

  15. RUBEN says:

    Buy@Cheap.Accupril” rel=”nofollow”>.…

    Buygeneric meds…

  16. BILLY says:

    accutane@for.sale” rel=”nofollow”>.…

    Buygeneric drugs…

  17. CARL says:

    Buy@Cheap.Aciphex” rel=”nofollow”>.…

    Buygeneric meds…

  18. ROY says:

    Cheap@Actonel.Without.Prescription” rel=”nofollow”>…

    Buynow it mou…

  19. CLIFTON says:

    Purchase@Cheap.Acular” rel=”nofollow”>..

    Buygeneric meds qmo…

  20. RODNEY says:

    Order@Cheap.Adalat” rel=”nofollow”>..

    Buygeneric drugs lzt…

  21. JEREMIAH says:

    Buy@Generic.Abilify.Without.Prescription” rel=”nofollow”>..

    Buynow it tly…

  22. MARION says:

    Buy@Generic.Acai” rel=”nofollow”>.

    Buygeneric drugs mby…

  23. JERRY says:

    Cheap@Generic.Acai” rel=”nofollow”>…

    Buydrugs without prescription vrg…

  24. JOEL says:

    Order@Cheap.Energy.Boost” rel=”nofollow”>..

    Buyno prescription wkp…

  25. MARION says:

    Order@Discount.Energy.Boost” rel=”nofollow”>..

    Buyno prescription gdt…

  26. MITCHELL says:

    Order@Cheap.Aciphex” rel=”nofollow”>..

    Buydrugs without prescription faj…

  27. BRUCE says:

    Purchase@Discount.Aciphex” rel=”nofollow”>..

    Buyno prescription jui…

  28. MILTON says:

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

    Buydrugs without prescription hak…

  29. BRYAN says:

    Order@Cheap.Actonel” rel=”nofollow”>.

    Buygeneric drugs nih…

  30. CARLOS says:

    Purchase@Actonel.Online” rel=”nofollow”>…

    Buygeneric drugs vbr…

  31. VINCENT says:

    Purchase@Cheap.Actonel” rel=”nofollow”>.

    Buydrugs without prescription xab…

  32. TRAVIS says:

    Order@Discount.Advair” rel=”nofollow”>..

    Buyno prescription tao…

  33. BILLY says:

    Get@Aggrenox.Online” rel=”nofollow”>…

    Buynow fjl…

  34. RENE says:

    Order@Cheap.Albenza” rel=”nofollow”>…

    Buynow it sxk…

  35. BARRY says:

    risperdal@vs.abilify” rel=”nofollow”>.

    Buygeneric drugs…

  36. RICHARD says:

    allegra@generic.purchase” rel=”nofollow”>…

    Buynow…

  37. gerard says:

    Search music now US Charts…

  38. GREG says:

    aspirin pain relief dog

    Buy_drugs without prescription…

  39. DERRICK says:

    nursing drug study website

    Buy_drugs without prescription…

  40. WADE says:

    planar warts

    Buy_drugs without prescription…

  41. KYLE says:

    stop smoking phlegm

    Buy_drugs without prescription…

  42. BYRON says:

    what foods anorexia eat

    Buy_drugs without prescription…

  43. CLIFTON says:

    nexium pepsin ac

    Buy_generic drugs…

Leave a Reply