Managed Debugging Assistant 'ContextSwitchDeadlock' has detected a problem in 'C:\xxxx.vshost.exe'

摘要:Managed Debugging Assistant 'ContextSwitchDeadlock' has detected a problem in 'C:\xxxx.vshost.exe'

使用執行訊模型為 [STAThread] 時,若系統進入長時間的等待作業(例如 sleep 三分鐘),就會發生此問題。

 

繁中錯誤訊息:
Managed Debugging Assistant 'ContextSwitchDeadlock' has detected a problem in 'C:\xxxx.vshost.exe'.
Additional Information: CLR 在過去 60 秒一直無法從 COM 內容 0x1f9600 轉換為 COM 內容 0x1f9770。擁有該目的內容/Apartment 的執行緒,很可能正在進行非提取等候或正在處理非常長的執行作業,而未提取 Windows 訊息。這種情況通常會對效能產生負面影響,甚至可能導致應用程式停止回應,或導致記憶體使用量持續隨時間而累積。若要避免這個問題,所有單一執行緒的 Apartment (STA) 執行緒都應該使用提取等候基本方法 (例如 CoWaitForMultipleHandles),並且在長時間的執行作業中定期提取訊息。

 

英文錯誤訊息:
Managed Debugging Assistant 'ContextSwitchDeadlock' has detected a problem in 'C:\xxxx.vshost.exe'.
The CLR has been unable to transition from COM context 0x1f9600 to COM context 0x1f9770 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.

 

解決方法:
因為 MDA (managed debugger assistant) 認為程式陷入 deadlock,所以會彈出這錯誤,
強制關閉:功能選單Debug -> Exceptions -> Managed Debugging Assistants, 取消選項 ContextSwitchDeadlock feature

但是當程式真的發生 deadlock 的時候,就無從得知錯誤了,慎用。

勤勞樸實