gpt4 book ai didi

c# - C# 中检测到 ContextSwitchDeadlock 错误

转载 作者:IT王子 更新时间:2023-10-29 03:40:26 25 4
gpt4 key购买 nike

我正在运行一个 C# 应用程序,在运行时出现以下错误:

The CLR has been unable to transition from COM context 0x20e480 to COM context 0x20e5f0 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.

谁能帮我解决这里的问题?

非常感谢。

最佳答案

程序的主线程已经忙于执行代码一分钟了。它没有处理它的正常职责,而是在消息循环中运行。当您在工作线程中使用 COM 服务器时,这是非法的:在您的主线程再次空闲之前,无法分派(dispatch)对其方法的调用。

它应该很容易看到,您的 UI 应该像门钉一样死。 Windows 应该用显示“无响应”的幽灵替换您的主窗口。关闭窗口将不起作用,没有任何点击事件有任何影响。

无论你的主线程在做什么,都应该由工作线程来完成。 BackgroundWorker 类对此很有用,您会在 MSDN Library 文章中找到它的许多使用帮助。如果您不知道主线程在做什么,请使用 Debug + Break All、Debug + Windows + Threads。

还有一个可能的原因:如果你使用的是RTM版本的VS2005,一定要安装service pack 1。

关于c# - C# 中检测到 ContextSwitchDeadlock 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2797677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com