Jannik Anker

.NET developer blog
posts - 197, comments - 14, trackbacks - 46

My Links

News



www.flickr.com
This is a Flickr badge showing photos in a set called Bornholm with a DSLR. Make your own badge here.

Article Categories

Archives

Post Categories

ASP.NET

Blogs

C#

MCMS

Personal web sites

SharePoint

Tricking out your apps (WinForms)

What I've learned today:

The final application startup tweak we'll discuss is preventing a second instance of the application from starting. Of course, some document-based applications make sense to run multiple times, but many utilities make no sense, or can even cause problems when run this way. It turns out that we can easily make a change to accomplish this. In Visual Basic, you can simply click the Make single instance application checkbox in Project Settings to enable single-instance mode. In C# (and VB for that matter), you can create the same effect in the Main method.

using System;
using System.Collections.Generic;

public class MyClass
{
    public static void Main()
    {
        bool exclusive;
        Mutex m = new Mutex(true, "C4F-TrickedOut", out exclusive);
        if( exclusive )
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 mainForm = new Form1();
            Application.Run();
        }
        else
        {
            MessageBox.Show("Another instance is already running.");
        }
    }
    
    
}



Very nice. More like this here (Coding4Fun)

Print | posted on Wednesday, May 03, 2006 12:18 PM | Filed Under [ C# VS.NET Microsoft VB.NET ]

Feedback

Gravatar

# re: Tricking out your apps (WinForms)

I've been struggling with click-once apps and single instance applications -- especially when it comes to applying commandline parameters to the second instance.

Solving the problem includes a update of the initialinstanceactivator from Chris Sells <a href="http://www.sellsbrothers.com/tools/genghis/">Genghis project</a> and a small bucket of remoting.

5/10/2006 8:09 AM | dalager
Gravatar

# re: Tricking out your apps (WinForms)

For newbies out there, to make this example work you need to add a using for the threading object, like this:

using System.Threading;
3/20/2008 10:38 PM | Roy Davis
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: