Use already running application

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?


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.


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.

链接地址: http://www.djcxy.com/p/51152.html

上一篇: 如何将参数发送到C#中正在运行的进程?

下一篇: 使用已运行的应用程序