tags:

views:

204

answers:

6

How to make it so if one copy of a program is running another won't be able to open?

Or better yet, how to make it so that if one copy is already running, then trying to run another copy will just act as if you maximized the original process?

+4  A: 

Scott Hanselman wrote a post on doing this sort of thing

Garry Shutler
+1 for using WindowsFormsApplicationBase class. Although if it is a console application you will need to use mutexes. Here's a nice example: http://www.ai.uga.edu/mc/SingleInstance.html
Darin Dimitrov
+1  A: 

You can use Mutex to make your app singleton. There are plenty of examples how to do it.

aku
+1  A: 

This article

True Single instance application - WinForms.NET

explains how to create a true single instance:

This article simply explains how you can create a windows application with control on the number of its instances or run only single instance. This is very typical need of a business application. There are already lots of other possible solutions to control this.

e.g. Checking the process list with the name of our application. But this methods don't seems to be a good approach to follow as everything is decided just on the basis on the application name which may or may not be unique all across.

using System;
using Microsoft.VisualBasic.ApplicationServices;

namespace Owf
{
  public class SingleInstanceController
    : WindowsFormsApplicationBase
  {
    public SingleInstanceController()
    {
      // Set whether the application is single instance
      this.IsSingleInstance = true;

      this.StartupNextInstance += new 
        StartupNextInstanceEventHandler(this_StartupNextInstance);
    }

    void this_StartupNextInstance(object sender, 
                      StartupNextInstanceEventArgs e)
    {
      // Here you get the control when any other instance is 
      // invoked apart from the first one. 
      // You have args here in e.CommandLine.

      // You custom code which should be run on other instances
    }

    protected override void OnCreateMainForm()
    {
      // Instantiate your main application form
      this.MainForm = new Form1();
    }
  }
}

Change you main function this way:

[STAThread]
static void Main()
{
  string[] args = Environment.GetCommand
  SingleInstanceController controller = new SingleInstanceController();
  controller.Run(args);
}
splattne
A: 

The Microsoft.VisualBasic.dll assembly contains a class 'WinformsFormsApplicationBase' which contains some functionality like the thing you want. You can use this class in a C# application as well.

Just create a class which inherits from this class. Set the SingleInstance property to true and override the necessary methods.

Offcourse, this means that you have a reference to the VisualBasic.dll assembly, which could be seen as a disadvantage, but, I think it is by far the most simple and easiest solution.

More info can be found here

Frederik Gheysels
A: 

Check out this question mate: Other Question

Spence
+2  A: 

Your best option is to use a named mutex. These articles explain the design pretty well and provide all the necessary code:

http://sanity-free.org/143/csharp_dotnet_single_instance_application.html

http://iridescence.no/post/CreatingaSingleInstanceApplicationinC.aspx

Extending this to maximise the main window of the running application should be a simple alteration to either of the examples provided.

Noldorin