The exception is clear enough, but you are trying to use a weird method of getting exception information, to say the least.
First, about the exception. The named Mutex is rarely used directly and only makes sense across two different processes. Are you sure you are not trying to used it inside the same process? Now, with several processes: you create the instance of the Mutex in one process and open it in all other processes. There are two constructors which are agnostic to the fact if the Mutex instance currently owned or not. Usually, you don't need to know that, but in three of the constructors you can get get this information form the "out" Boolean parameter.
See those constructors here:
http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx[
^].
By some reason, you are using the last constructor where you need to pass the first parameter correctly. You should not use this kind of constructor. If you claim that this constructor should open a Mutex already opened in a different thread (which makes sense only if this thread is in the different process but could work if it was in the same one) and it this is not so, you get this exception.
As it's hard to guarantee, I suggest you use constructors
Mutex.Mutex(string)
which is agnostic to this detail. In all other cases, you should guarantee that.
http://msdn.microsoft.com/en-us/library/system.threading.waithandlecannotbeopenedexception.aspx[
^].
Now, to get exception information, catch either all exception of the type
System.Exception
or just the specific exception
on the very top of the stack of each thread, it catch all the exceptions no matter how deep was the call. The instance if the exception will be passes to the handler through the catch parameter. For this instance, examine
Exception.InnerException
, recursively, and — important —
Exception.Stack
. Stack data is just a string; if debug information is available, it will report to you code file names and lines where the exception was thrown/propagated. Having the line numbers, you can run there with the debugger, etc.
—SA