Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContextSwitchDeadlock Was Detected error in C#

I am running a C# application, and during run-time I get the following error:

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.

Can anyone please help me out with the problem here?

Thanks a lot.

like image 525
assassin Avatar asked May 09 '10 13:05

assassin


People also ask

How to solve ContextSwitchDeadlock was detected in c#?

Simply select Exceptions from the Debug menu in the visual studio 2005 window , the Edxception Dialog box wil popup , select the Managed Debugging Assistants Exception Node , then select ContextSwitchDeadlock and remove the select from Thrown column .

How do I turn off ContextSwitchDeadlock?

Expand the MDA node and uncheck the box against contextswitchdeadlock . Hope that solves your problem.

What is context switch deadlock?

The contextSwitchDeadlock managed debugging assistant (MDA) is activated when a deadlock is detected during an attempted COM context transition.


2 Answers

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.

like image 91
Hans Passant Avatar answered Oct 05 '22 09:10

Hans Passant


To find which operation is blocking the context switch and causing the contextSwitchDeadlock MDA to be displayed, you can use the following steps. Note that I will be referring to Visual Studio 2012.

  1. Reproduce the error. This could involve some trial and error.
  2. Click 'OK' rather than 'Continue' in the Managed Debugging assistant which is displayed.
  3. Ensure that the Debug Location toolbar is active by right clicking on the toolbar docking region and selecting 'Debug Location'. You should see a drop down list labelled 'Thread' in the toolbar if it is active.
  4. The selected item in the Thread drop down list should be a thread other than the main thread as it will be a background thread that is complaining that the main thread is hogging all of the attention. Select the main thread in the drop down list.
  5. You should now see the code that is blocking the context switch in the code editor.

Assuming that you decide against moving the resource intensive operation off your main thread - take a look at some of the other answers and comments here before you do - you have the following options to disable the Managed Debugging Assistants.

Within the Visual Studio Debugger

  1. You can disable an MDA directly in the MDA dialog that is displayed when the error occurs by unchecking 'Break when this exception type is thrown'.
  2. With Exception Settings dialog using the instructions below from MSDN.

...on the Debug menu, click Exceptions. (If the Debug menu does not contain an Exceptions command, click Customize on the Tools menu to add it.) In the Exceptions dialog box, expand the Managed Debugging Assistants list, and then clear the Thrown check box for the individual MDA.

Outside the Visual Studio Debugger

  1. Registry Key (Machine Wide, All MDAs affected)
  2. Environment Variable (Machine Wide, MDAs can be specified)
  3. Application Configuration Settings (Application scope, MDAs can be specified)

Note: One of the first two options must be set to 1 for the third to have any effect.

In my case, the problem was a call to ObjectContext.SaveChanges() in the Entity Framework within a console application. With the MTAThreadAttribute applied to the Main() method the ContextSwitchDeadlock exception was no longer raised. I am unfortunately unsure of the full affects of this change.

like image 26
Scott Munro Avatar answered Oct 05 '22 07:10

Scott Munro