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.

Leave a Comment