I understand that Mutex can be used to allow only one instance of my app starting on a single machine. This means that no matter how many desktops are used by an arbitrary number of users, there can be at most one instance of the program running.
Now, instead of only allowing one instance, I want to allow for at most two, nothing more. How to do it? I tried code such as this:
public static bool MutexChecking()
{
bool createNew1;
// one set of GUID
new Mutex(false, "54A8F2C1-6C11-4ED4-8A62-AD7C1D9F7970", out createNew1);
if (createNew1)
return true;
bool createNew2;
//another set
new Mutex(false, "5E1C76D0-9897-412B-BD56-64A872F8FDE3", out createNew2);
return createNew2;
}
The above code works... sort of. It works in all the cases I have tested except this one:
- Open the program( let's call it
p1
) - Open the program again ( let's call it
p2
) - Close
p1
. - Open the program a third time (
p3
) - Expectation: during
p3
, the above method should return a true ( becausecreateNew1
should betrue
), but it returns a false ( becausecreateNew1
returnsfalse
).
Why is this so? Any idea how to fix this?