Context Switch Deadlock in Visual Studio

Class library usage, coding and language questions.
User avatar
Posts: 374
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Context Switch Deadlock in Visual Studio

Post by Aracknid » Mon Sep 11, 2017 3:20 pm


When I run my scripts from within Visual Studio, I occasionally get this Context Switch Deadlock break mode popping up. It doesn't always happen in the same place in the script or always happen in the script itself. Most of the time it works fine. I've just been noticing it more lately and thought I'd ask a few questions relating to it...
ContextSwitchDeadlock.jpg (83.98 KiB) Viewed 7881 times
Here is the full message:
ContextSwitchDeadlock occurred
Message: Managed Debugging Assistant 'ContextSwitchDeadlock' has detected a problem in 'C:\TFS\xxxx\Debug\EXPENSE_CCFEED_BAT_03.vshost.exe'.
Additional information: The CLR has been unable to transition from COM context 0xf74106f8 to COM context 0xf7410820 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.
1) It seems to always happen when I'm on a line of Ranorex code, such as a Find. Yes I have timeouts for the finds.
2) I have <STAThread()> around my main class of my script. Do I perhaps need to do more in other functions or classes that are called by this?
3) If nothing can be done in code, does anyone know how to turn this break off? I never care when I see it and always uncheck it and hit continue.



Posts: 297
Joined: Thu Feb 23, 2017 10:37 am
Location: Benedikt, Slovenia

Re: Context Switch Deadlock in Visual Studio

Post by McTurtle » Thu Sep 14, 2017 3:11 pm

Hello Aracknid,

I was able to find the following link: Visual Studio: ContextSwitchDeadlock
It is explained under that link why this can happen, what the consequences could be and how to disable it :)
I hope this helps.