Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the code below an exeption of WaitHandleCannotBeOpenedException is thrown. but its not enough information to track the error. So I want to use the method

C#
public WaitHandleCannotBeOpenedException(String message,Exception innerException

)


to know what is the real error message and the inner eception which causes it. So where and How do I use this method ....

C#
try
           {
               //try to get the existing mutex
               _mutex = Mutex.OpenExisting(MUTEX_NAME, MutexRights.FullControl);
           }

catch (Exception ex)
            {
                //if there was an exception, the mutex didn't exist so create it
                MutexSecurity mutexSecurity = new MutexSecurity();
                mutexSecurity.AddAccessRule(new MutexAccessRule("Everyone", MutexRights.FullControl, AccessControlType.Allow));
                bool createdNew;
                _mutex = new Mutex(false, MUTEX_NAME, out createdNew, mutexSecurity);
            }
Posted
Updated 12-Dec-11 5:31am
v2

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
 
Share this answer
 
Why don't you just look at the exception you are given in the catch block? It will contain the InnerException property which should give you all the info you need.
 
Share this answer
 
Comments
prat7 12-Dec-11 13:02pm    
The problem is with the mutex...mutex handle not found

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900