C# Visual Studio:ContextSwitchDeadlock
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/578357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio: ContextSwitchDeadlock
提问by SeaDrive
I have been getting an error message that I can't resolve. It originates from Visual Studio or the debugger. I'm not sure whether the ultimate error condition is in VS, the debugger, my program, or the database.
我收到一条我无法解决的错误消息。它源自 Visual Studio 或调试器。我不确定最终的错误条件是在 VS、调试器、我的程序还是数据库中。
This is a Windows app. Not a web app.
这是一个 Windows 应用程序。不是网络应用程序。
First message from VS is a popup box saying: "No symbols are loaded for any call stack frame. The source code can not be displayed." When that is clicked away, I get: "ContextSwitchDeadlock was detected", along with a long message reproduced below.
来自 VS 的第一条消息是一个弹出框,上面写着:“没有为任何调用堆栈帧加载符号。无法显示源代码。” 当点击它时,我得到:“检测到 ContextSwitchDeadlock”,以及下面复制的长消息。
The error arises in a loop that scans down a DataTable. For each line, it uses a key (HIC #) value from the table as a parameter for a SqlCommand. The command is used to create a SqlDataReader which returns one line. Data are compared. If an error is detected a row is added to a second DataTable.
该错误出现在向下扫描 DataTable 的循环中。对于每一行,它使用表中的一个键 (HIC #) 值作为 SqlCommand 的参数。该命令用于创建一个返回一行的 SqlDataReader。数据进行比较。如果检测到错误,则会向第二个 DataTable 添加一行。
The error seems to be related to how long the procedure takes to run (i.e. after 60 sec), not how many errors are found. I don't think it's a memory issue. No variables are declared within the loop. The only objects that are created are the SqlDataReaders, and they are in Using structures. Add System.GC.Collect() had no effect.
该错误似乎与程序运行所需的时间(即 60 秒后)有关,而不是发现了多少错误。我不认为这是内存问题。循环中没有声明变量。唯一创建的对象是 SqlDataReaders,它们在 Using 结构中。添加 System.GC.Collect() 没有效果。
The db is a SqlServer site on the same laptop.
db 是同一台笔记本电脑上的 SqlServer 站点。
There are no fancy gizmos or gadgets on the Form.
表单上没有花哨的小玩意或小工具。
I am not aware of anything in this proc which is greatly different from what I've done dozens of times before. I have seen the error before, but never on a consistent basis.
我不知道这个过程中有什么与我之前做过的几十次大不相同的事情。我以前见过这个错误,但从来没有在一致的基础上。
Any ideas, anyone?
任何想法,任何人?
Full error Text:The CLR has been unable to transition from COM context 0x1a0b88 to COM context 0x1a0cf8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
完整错误文本:CLR 无法从 COM 上下文 0x1a0b88 转换到 COM 上下文 0x1a0cf8 60 秒。拥有目标上下文/单元的线程很可能在执行非泵等待或在不泵 Windows 消息的情况下处理非常长时间运行的操作。这种情况通常会对性能产生负面影响,甚至可能导致应用程序变得无响应或内存使用量随着时间的推移不断累积。为避免此问题,所有单线程单元 (STA) 线程都应使用泵等待原语(例如 CoWaitForMultipleHandles)并在长时间运行的操作期间定期泵送消息。
采纳答案by Pedro
The ContextSwitchDeadlock
doesn't necessarily mean your code has an issue, just that there is a potential. If you go to Debug > Exceptions
in the menu and expand the Managed Debugging Assistants
, you will find ContextSwitchDeadlock
is enabled. If you disable this, VS will no longer warn you when items are taking a long time to process. In some cases you may validly have a long-running operation. It's also helpful if you are debugging and have stopped on a line while this is processing - you don't want it to complain before you've had a chance to dig into an issue.
这ContextSwitchDeadlock
并不一定意味着您的代码有问题,只是有潜力。如果您转到Debug > Exceptions
菜单中的 并展开Managed Debugging Assistants
,您会发现ContextSwitchDeadlock
已启用。如果您禁用此功能,当项目需要很长时间来处理时,VS 将不再警告您。在某些情况下,您可能有一个长时间运行的操作。如果您正在调试并且在处理过程中停在一条线上,这也很有帮助 - 在您有机会深入研究问题之前,您不希望它抱怨。
回答by Rob Walker
It sounds like you are doing this on the main UI thread in the app. The UI thread is responsible for pumping windows messages as the arrive, and yet because yours is blocked in database calls it is unable to do so. This can cause problems with system wide messages.
听起来您是在应用程序的主 UI 线程上执行此操作。UI 线程负责在到达时泵送 Windows 消息,但由于您的消息在数据库调用中被阻塞,因此无法执行此操作。这可能会导致系统范围的消息出现问题。
You should look at spawning a background thread for the long running operation and putting up some kind of "I'm busy" dialog for the user while it happens.
您应该考虑为长时间运行的操作生成一个后台线程,并在它发生时为用户提供某种“我很忙”对话框。
回答by Pedro
If you don't want to disable this exception, all you need to do is to let your application pump some messages at least once every 60 seconds. It will prevent this exception to happen. Try calling System.Threading.Thread.CurrentThread.Join(10) once in a while. There are other calls you can do that let the messages pump.
如果您不想禁用此异常,您需要做的就是让您的应用程序至少每 60 秒抽取一次消息。它将防止这种异常发生。尝试偶尔调用 System.Threading.Thread.CurrentThread.Join(10) 。您还可以执行其他调用,让消息不断涌现。
回答by Spence
As Pedro said, you have an issue with the debugger preventing the message pump if you are stepping through code.
正如 Pedro 所说,如果您正在逐步执行代码,则调试器会阻止消息泵出现问题。
But if you are performing a long running operation on the UI thread, then call Application.DoEvents() which explicitly pumps the message queue and then returns control to your current method.
但是,如果您在 UI 线程上执行长时间运行的操作,则调用 Application.DoEvents() 显式抽取消息队列,然后将控制权返回给您当前的方法。
However if you are doing this I would recommend at looking at your design so that you can perform processing off the UI thread so that your UI remains nice and snappy.
但是,如果您这样做,我建议您查看您的设计,以便您可以在 UI 线程之外执行处理,从而使您的 UI 保持美观和活泼。
回答by Ewan
The above solution is good in some scenarios but there is another scenario where this happens when you are unit testing and you try to "Debug Selected Tests" from the Test Explorer when you solution is not set to Debug.
上述解决方案在某些情况下很好,但还有另一种情况,当您进行单元测试并且当您的解决方案未设置为“调试”时,您尝试从测试资源管理器“调试选定的测试”时会发生这种情况。
In this case you need to change your solution from Release or whatever it is set to to Debug in this case. If this is the problem then changing "ContextSwitchDeadlock" won't really help you.
在这种情况下,您需要将解决方案从 Release 或在这种情况下设置为 Debug 的任何内容更改。如果这是问题,那么更改“ContextSwitchDeadlock”将不会真正帮助您。
I missed this myself because the error message was so nasty I didn't check the obvious thing which was the Debug setting!
我自己错过了这一点,因为错误消息太糟糕了,我没有检查明显的调试设置!
回答by kahonmlg
In Visual Studio 2017 Spanish version.
在 Visual Studio 2017 西班牙语版本中。
"Depurar" -> "Ventanas" -> "Configuración de Excepciones"
"Depurar" -> "Ventanas" -> "Configuración de Excepciones"
and search "ContextSwitchDeadlock". Then, uncheck it. Or shortcut
并搜索“ContextSwitchDeadlock”。然后,取消选中它。或者快捷方式
Ctrl+D,E
Ctrl+D,E
Best.
最好的事物。
回答by KR Akhil
You can solve this by unchecking contextswitchdeadlock from
您可以通过取消选中 contextswitchdeadlock 来解决此问题
Debug->Exceptions ... -> Expand MDA node -> uncheck -> contextswitchdeadlock
Debug->Exceptions ... -> Expand MDA node -> uncheck -> contextswitchdeadlock
回答by Hassan Rahman
回答by dunwan
I was getting this error and switched the queries to async (await (...).ToListAsync()). All good now.
我收到此错误并将查询切换为异步(await (...).ToListAsync())。现在一切都很好。