A C# code retrieves a property value from a C++ dll and this retrieval blocks the current thread until the dll return开发者_StackOverflow中文版s the value. In debug mode, when the retrieval takes a long time, the MDA throws a ContextDeadlockSwitch.
I suppose it's not possible to catch ContextDeadlockSwitch at runtime, but is there a way that the a mechanism similar to MDA can throw a similar exception that C# can catch to detect this deadlock?
The reason is to let C# code prompt to the user to either continue waiting a few seconds more or forcibly kill and restart the app.
You could perform the call into the C++ dll in a different thread, using a wait with timeout on a ManualResetEvent to signal completion; if the timeout is reached you can ask the user if they want to continue. Note that if the DLL's COM object is in a STA, you may need to create the object in an apartment reserved for it and marshall across, in order to prevent it from freezing your GUI thread when it goes out to lunch.
精彩评论