2

Possible Duplicate:
What is the correct way to create a single instance application?
How to implement single instance per machine application?

I have a Winform-app which takes one parameter as an argument.

static class Program
{

    [STAThread]
    static void Main(string[] args)
    {           
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Gauges(args));
    }
}

This program is executed by another application several times a day.

Is it possible to check if my programm is already running and if so, can I use the running instance with the latest parameter?

Community
  • 1
  • 1
Koen
  • 2,501
  • 1
  • 32
  • 43
  • Implement a check by getting all processes and compare for example the process name to the one you're starting. If it's present in the list just `return;`. If it isn't - start it – t3hn00b Jan 28 '13 at 10:37
  • 1
    @t3hn00b Just use a mutex. No need to check all processes then. – Lloyd Jan 28 '13 at 10:40
  • @Lloyd yeah, that's correct - I wasn't familiar with the Mutex class before I saw your post. – t3hn00b Jan 28 '13 at 10:42
  • possible duplicate of [What is the correct way to create a single instance application?](http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application) or [Prevent multiple instances of a given app in .NET?](http://stackoverflow.com/questions/93989/prevent-multiple-instances-of-a-given-app-in-net) – Steve B Jan 28 '13 at 10:44

2 Answers2

2

You can stop multiple copies of your application running using a mutex. There's a very good article on using mutexes here: http://sanity-free.org/143/csharp_dotnet_single_instance_application.html

As to passing along the parameter to an already running process, you would need to implement some form of IPC to notify the running instance of the new parameter. You could use a number of solutions like sockets, named pipes etc.

Lloyd
  • 29,197
  • 4
  • 84
  • 98
2

Is it possible to check if my programm is already running

You can use Mutex inside your application

bool alreadyPresent  =false;
using (Mutex mutex = new Mutex(true, "YourAppName", out alreadyPresent  ))
{
    if(alreadyPresent ) {
       //APP ALREADY PRESENT 
    }
}

Mutex is OS artifact, so, different instances of your application (executable) cann access the same object.

can I use the running instance with the latest parameter?

It depends how do you manage your app. You can use some IPC mechanism to communicate requeired parameter to already running process.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Third parameter of `Mutex` constructor is actually `createdNew`. If it sets the parameter to `false`, then the application (or another using the same mutex name) is already running. – JosephHirn Jan 28 '13 at 14:41
  • I understand the Mutex. Now need to figure out the IPC-stuff. Thx. – Koen Jan 28 '13 at 15:35
  • @Koen: can use [NamedPipe](http://www.codeproject.com/Tips/441841/Csharp-Named-Pipes-with-Async), but there are also others. This is just an example. – Tigran Jan 28 '13 at 15:39