1

I know that this question could be a bit silly, but I am new to c#, and to coding in general. I recently made an app, and I want to restrict it to have only once instance of it running at a time, so that a user can't launch it multiple times. I found this answer by michalczerwinski on stackoverflow:

[STAThread]
static void Main()
{
    bool result;
    var mutex = new System.Threading.Mutex(true, "UniqueAppId", out result);

    if (!result)
    {
        MessageBox.Show("Another instance is already running.");
        return;
    }

    Application.Run(new Form1());

    GC.KeepAlive(mutex);                // mutex shouldn't be released - important line
}

Can anyone tell me where I am supposed to add this? I've tried adding it everywhere inside the Form1.cs, but it ain't working.

Bosko Sinobad
  • 109
  • 2
  • 9
  • By default Visual Studio puts the `Main` method in a file called Program.cs. Open that file and copy into the `Main` method the contents of this `Main` method and you should be good to go (unless you've renamed `Form1` or something like that). – adv12 Jan 05 '16 at 17:41
  • @adv12 It worked. Thanks for your help! – Bosko Sinobad Jan 05 '16 at 17:48

3 Answers3

3
static void Main()

Is the name of the main function. You should add this to the "Program.cs" file (standard name) to this specific function (before everything else in the function).

And it is a good practice to free resources. Therefore it would be good to add mutex.Dispose(); or using (mutex) { } at the end of the function (not both, only one of these options).

Julo
  • 1,102
  • 1
  • 11
  • 19
2

(update: I obviously did not read your question carefully, as you have a way for a single instance and just wanted to know where to add it. Anyway, I think this answer is still helpful as it provides a good way for single-instance-apps with further possibilities and no need to take care of mutexes in your own code.

You can derive a class from WindowsFormsApplicationBase, set the IsSingleInstance property to true and override the OnCreateMainForm method. You will need to reference Microsoft.VisualBasic.dll to your project.

Here is a good example of how to use WindowsFormsApplicationBase to handle further process starts and invoke the already running instance.

Inherited class:

public class App : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
    public App()
    {
        // Make this a single-instance application
        this.IsSingleInstance = true; 
        this.EnableVisualStyles = true;
    }

    protected override void OnCreateMainForm()
    {
        // Create an instance of the main form 
        // and set it in the application; 
        // but don't try to run() it.
        this.MainForm = new Form1();
    }
}

And your main method would now look like this:

static void Main(string[] args)
{
    App myApp = new App();
    myApp.Run(args); 
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
1

For me, the Mutex solution didn't work, so instead I've used the Process method. It basically checks if there is an other instance running. You have to place it in your Program.cs file, before Application.Run().

if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)).Length > 1)
        {
            MessageBox.Show("Another instance of this program is already running. Cannot proceed further.", "Warning!");
            return;
        }

This is the easiest method so far, at least for me.

Edit : This method has originally been posted here.

ShiningLea
  • 132
  • 1
  • 12