在运行时检测 ContextDeadlockSwitch
C# 代码从 C++ dll 检索属性值,并且此检索会阻塞当前线程,直到 dll 返回该值。在调试模式下,当检索需要很长时间时,MDA 会抛出 ContextDeadlockSwitch。
我认为不可能在运行时捕获 ContextDeadlockSwitch,但是有没有一种方法可以让类似于 MDA 的机制抛出类似的异常,而 C# 可以捕获该异常来检测此死锁?
原因是让 C# 代码提示用户要么继续等待几秒钟,要么强行终止并重新启动应用程序。
A C# code retrieves a property value from a C++ dll and this retrieval blocks the current thread until the dll returns 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在不同的线程中执行对 C++ dll 的调用,在
ManualResetEvent 表示完成;如果超时,您可以询问用户是否要继续。请注意,如果 DLL 的 COM 对象位于 STA 中,则您可能需要在为其保留的单元中创建该对象并进行编组,以防止它在出去吃午饭时冻结您的 GUI 线程。
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.