Visual Studio: Contextswitchdeadlock

Visual Studio: ContextSwitchDeadlock

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 Was Detected error in C#

The main thread of your program has been busy executing code for a minute. It is not taking care of its normal duties, pumping the message loop. That's illegal when you use COM servers in a worker thread: calls to their methods cannot be dispatched until your main thread goes idle again.

It should be readily visible, your UI should be dead as a door nail. Windows should have replaced your main window with a ghost that displays "Not Responding". Closing the window won't work, no click events have any effect.

Whatever your main thread is doing should be done by a worker thread instead. The BackgroundWorker class is good for that, you'll find many usage help in the MSDN Library article for it. Use Debug + Break All, Debug + Windows + Threads if you have no idea what the main thread is doing.

One more possible cause: be sure to install service pack 1 if you are using the RTM version of VS2005.

.NET - ContextSwitchDeadlock was detected

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.

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.

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.

What would cause/how can I prevent a ContextSwitchDeadlock?

The ContextSwitchDeadlock is a "Managed debugging assistant" in Visual Studio and is one of many tools that the Visual Studio debugger gives you additional aid when debugging an application.

I am not 100% positive what kind of heuristics it uses to figure out when to trigger but I know some, and this is also described in the tool window itself:

The current thread is not currently running code or the call stack could not be obtained.

This last part is what usually trips up Visual Studio and this is usually something that happens when a managed (.NET) program calls into COM or P/Invoke. When this is done, Visual Studio is no longer able to look at the program and figure out what it is doing, and when this happens to take more than 60 seconds, it figures something has gone awry.

If your program is doing the above, calling into COM, or using P/Invoke, or something involving an external non-managed (.NET) code, and this external code takes more than 60 seconds to execute, then the dialog is basically a false positive. If you know that this is benign, as in, "yes, it calls external code, and yes, this may sometimes take more than 60 seconds" then you can remove the check lower in that image:

[ ] Break when this exception type is thrown

Note that it is not necessarily an exception though, this is more Visual Studio just raising a red flag on suspicious (to it) behavior. If you know this is OK, just tell it to stop raising that flag.

Note that this has nothing to do with the other real exceptions you mention in the question, only the big ContextSwitchDeadlock dialog.

ContextSwitchDeadlock was detected

You can ignore that exception (I've run into this before myself for long running methods).

  1. Hold ctrl+alt+e
  2. Click Find
  3. Type ContextSwitchDeadlock and press Enter
  4. Uncheck Thrown in the table
  5. Close the Exceptions configuration window by pressing OK

CustomXMLParts.Add slow due to ContextSwitchDeadlock

The context switch deadlock can show up when debugging a long running process. For the most part you can ignore it if the process is expected to be long running.

see previous stackoverflow answer

Visual Studio 2008 ContextSwitchDeadlock with log4net and NHibernate

When using the DebugAppender all the entities in the databases are loaded and all its data written to the debug output. That was causing the ContextSwitchDeadlock MDA since it tookmore than 60 seconds to run.

Disabling the DebugAppender solved my problem.

Thanks to Mauricio Scheffer for the tip.



Related Topics



Leave a reply



Submit