.NET - ContextSwitchDeadlock检测NET、ContextSwitchDeadlock

2023-09-02 21:30:56 作者:云深

我有一类在C#(.NET 3.5厘,VS2010),它执行复杂的comuptations这ussually需要很长的时间。一分钟后,有抛出的异常,这ContextSwitchDeadlock检测。异常是局部的,以我的非英语语言,所以我不能复制粘贴,但含义如下: ¨ CLR模块无法从上下文COM ...上下文COM ... 60秒过渡。 Subproces拥有目标上下文/公寓可能是做一个非抽水等待或处理一个很长的运行操作,不抽的Windows系统消息。

i have a class in c# (.net 3.5 cp, vs2010) which executes complex comuptations which ussually take a long time. After a minute there is thrown an exception, that ContextSwitchDeadlock was detected. Exception is localised, to my non english language so i cant copy paste, but meaning is following: ¨ CLR module could not transition from context COM ... to context COM ... for 60 seconds. Subproces which owns target context/apartment is probably doing a non-pumping wait or processing a very long running operation without pumping Windows system messages.

Basicaly,它看起来像我的应用程序的计算和不响应的窗口时间长和Visual Studio将其关闭并报告problable僵局。

Basicaly, it looks like my application is computing and not responding to windows for a long time and visual studio shuts it down and reports problable deadlock.

我试图做一些研究,并发现了两个解决方案:

I was trying to do some research and found two solutions:

禁用在Visual Studio debbuger一些选项来检测死锁。也许不适合我,因为它只woirks用于调试。

Disable some option in visual studio debbuger to detect deadlocks. Dost not work for me because it woirks only for debugging purposes.

调用一些的DoEvents方法,但它是为Windows Forms和不WPF和我使用WPF。

Call some DoEvents method, but it was for windows forms and not WPF and i am using WPF.

还有人建议建立单独的线程,但我完全新线程和不知道我应该怎么做。有什么建议吗?

There was also suggestion to create separate thread, but i am complete new to threading and dont know what should i do. Any suggestions please?

推荐答案

这仅仅是一个从托管调试助手(MDA)的警告。您的code违反了单线程单元(STA)线程pretty的硬性要求,他们不能阻止长时间。该警告是真实的就够了,阻塞UI线程很容易造成僵局。但是,在你的情况的解释很简单,它只是紧张,因为这是繁忙的计算,而不是因为它实际上被阻止。 MDA的不能分辨出来。

It is just a warning from a Managed Debugging Assistant (MDA). Your code is violating a pretty hard requirement for Single Threaded Apartment (STA) threads, they are not allowed to block for long periods. The warning is real enough, blocking the UI thread can easily cause deadlock. But the explanation in your case is simple, it just goes catatonic because it is busy computing, not because it actually blocked. The MDA can't tell the difference.

您可以关闭使用调试+异常报警,打开托管调试助手节点的勾去掉ContextSwitchDeadlock。

You can turn off the warning with Debug + Exceptions, open the Managed Debugging Assistants node and untick ContextSwitchDeadlock.

这仍然让用户与她桌面上的一个窗口,是向世界死,不完全是一个很好的用户体验。而且它可以有副作用,造成其他程序时,他们将消息发送到顶层窗口没有响应。

That still leaves the user with a window on her desktop that is dead to the world, not exactly a great user experience. And it can have side-effects, causing other programs to become unresponsive when they send messages to toplevel windows.

您确实需要使用线程要真正解决这个问题。看一看BackgroundWorker的,这是有据可查的MSDN库和其他许多地方

You do need to use threading to really solve this problem. Have a look at BackgroundWorker, it is well documented in the MSDN Library and many other places.

 
精彩推荐
图片推荐