Resolution: Context Switch Deadlock was detected !!!

Posted: April 10, 2012 in .NET Framework
Tags: , ,

When I debug my solution which has an implementation of exporting data in a data set into an excel work book (while looping through the data set) it gives the following error in VS IDE,

The CLR has been unable to transition from COM context 0x4c4b68 to COM context 0x4c4cd8 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.

 

 

 

 

 

 

 

 

 

 

 

It’s just saying that I’m doing an operation which takes more than 60 seconds to operate on the UI thread.

Resolution: Though, this is really bad it can resolve the issue by appending Application.DoEvents() statement somewhere within the loop.

Application.DoEvents()

However, the best way of resolving this issue is to perform the time-consuming operation on another thread.

The root cause: When we stopped the executing in a break point, it just stopped the managed code. However, the un-managed code is still running and in this case, it might have stopped at a time out. At this stage, the un-managed code execution has completely stopped is not being able to understand it has stopped by the managed debugger. Now the managed code sees this as a deadlock situation.

What does Application.DoEvents() method really do?

Application.DoEvents() method will process all the messages that are currently in the message queue. In fact, when running a win form, it creates a new form and waits for events to handle. When the form handles an event, it always processes all the code associated with that event and all the other events will have to wait in the queue. In the time of handling these events, your code won’t respond. In such a case, if you call Application.DoEvents() then your application can handle the other events as well without being waiting for the running event to complete its execution.

Referencehttp://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents(v=vs.71).aspx

Comments
  1. linda says:

    thanks a lot

  2. Neha says:

    Thanks.. Its really works…

  3. kikin says:

    gracias compañero me acabas de aliviar de un estrés tremendo jajaja tenia horas batallando con este problema 🙂

Leave a comment