-1

I was playing with Mutex to understand if it can be used to know whether Microsoft Word is currently running in a computer with logged in user session. I tried below examples but none of them worked.

Ex1:

using (Mutex mutex = new Mutex(false, "WINWORD.exe"))
{
    if (mutex.WaitOne(0, false))
    {
        Console.WriteLine("Not running"); //Always printing                  
    }
    else
    {
        Console.WriteLine("Already running");
    }
}

Ex2:

bool createdNew;
Mutex m = new Mutex(false, "WINWORD.exe", out createdNew);
if (createdNew)
{
    Console.WriteLine("Not running"); //Always printing
}
else
{
    Console.WriteLine("Already running");
}

In both the example, it's printing "Not running", although I can see Word in running in my computer. Is it a limitation of Mutex? Or am I missing something here?

Prakash Tripathi
  • 469
  • 6
  • 12
  • 2
    Besides this being the wrong tool for the job (as nvoit's answer indicates), you also need to consider what question you actually need answering. Do you care if Word is running *on this machine*, or care if Word is running *in the same session*? Windows does support having multiple users logged in with programs running, but when you're logged in as user A, finding out that Word is running for user B a) requires quite a lot of permissions and b) probably doesn't help you solve your overall problem (which is what, by the way?) – Damien_The_Unbeliever Apr 20 '16 at 06:22
  • Hi Damien, I care If word is running in the same session. Updated the question as well. – Prakash Tripathi Apr 20 '16 at 08:26
  • Hi Damien, I hope that after updating title you would revert you vote down. – Prakash Tripathi Apr 20 '16 at 08:39
  • I haven't voted on your question at all, yet. – Damien_The_Unbeliever Apr 20 '16 at 08:42

1 Answers1

4

A mutex is not used to check if another program is already running.

A mutex is an element that helps with multiple tasks (either multithreading or multiprocessing), because it can be owned mutually exclusive by only one party and can be waited on for the other party to release it.

You can use a mutex to communicate simple yes/no statements between your own programs. The first that starts owns the mutex and the others, seeing it is already owned by somebody else, will know the same program is already started. That is probably how you came to believe your code would work. But that only works if all programs support it. The examples you saw were always checking if their own program was already running.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thanks nvoigt for the answer. So you mean that I can use Mutex to check if another instance of my own program (same exe) is running or not right? To check if Word is already running, I should be running code of Word at the same time. Please correct me if I am wrong. – Prakash Tripathi Apr 20 '16 at 15:55
  • You can only check, if a mutex is already in use. So when you have access to the code of both programs, you could use a mutex to tell when one of them is running. As Word is not your product and you cannot make Microsoft change their code to use a mutex of your choice, there is no way to check if Word is running with a mutex. – nvoigt Apr 20 '16 at 16:02