Question

How can I use mutex to find first process by GUID and call function in it ? Like in web browser when you try to start it second time it just open a new window.

// appGuid is const string containing app GUID
using (Mutex mutex = new Mutex(false, "Global\\" + appGuid))
{
    if (!mutex.WaitOne(0, false))
    {
        // CODE TO FIND AND CALL FIRST PROCESS AND THEN EXIT
        Environment.Exit(0);
    }
    else
    {
        App app = new App();
        app.InitializeComponent();
        app.Run();
    }
}
Was it helpful?

Solution

I think there are a lot of ways to tackle the "oh you opened a second instance, but we only allow one, so lets proxy your request to the first instance" problem.

  • You could have the host process be listening on a socket and when the mutex detects that its already been opened call into the listening socket. Maybe use something as simple as .NET remoting, no real need to go full blown WCF here or raw socket writing
  • You could also have a shared memory buffer and write using shared memory. But this will require synchronization between the two processes, so it can be kinda messy
  • You could just trigger a file and have the host process be listening for file events in a known location. This is probably the easiest, most low-tech solution

That's 3 ideas, there are probably more

But, if you just want to get process information why not just look up the process by name?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top