0

I am developing an application that calls a web service, which deletes information from a database (the web service was developed by a third party vendor). On the first run approximately 100,000 records are deleted.

I have tested the routine a few times and this appears in Visual Studio occasionally:

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

I assume that the web service is taking more than sixty seconds to pass control back to the .NET Forms app. Please see the following quote from the message: "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". As this is a Windows Forms app, does this mean that I do not need to do anything to allow for this?

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • Is the code doing the deletion running on the UI thread? If so try moving it to a background thread. – dbasnett Oct 25 '13 at 11:53
  • It is running on the main thread. Is that what you mean by the UI thread? Is this warning anything I should be concerned about? Than – w0051977 Oct 25 '13 at 13:13
  • Unless you have started another thread somehow then you only have one thread, and it is the one in control of the UI. If you don't care about the UI being responsive then don't worry about it, but most users don't like it. If you search the MSDN documentation you can turn this warning off. – dbasnett Oct 25 '13 at 13:27
  • dbasnett, thanks again. It is a windows scheduled task that calls the form so users don't use this app. Therefore I can ignore it. Do you know of any links that explain this error (I have Googled). Please post an answer so I can give some credit. – w0051977 Oct 25 '13 at 13:35
  • possible duplicate of [ContextSwitchDeadlock Was Detected error in C#](http://stackoverflow.com/questions/2797677/contextswitchdeadlock-was-detected-error-in-c-sharp) – Hans Passant Oct 28 '13 at 14:12
  • And this one: http://stackoverflow.com/questions/8106159/net-contextswitchdeadlock-was-detected/8106274#8106274 – Hans Passant Oct 28 '13 at 14:13
  • @Hans Passant, thanks. Your answer to the question in your link above suggests using the BackgroundWorker thread. My form app is called via the windows scheduler i.e. it is a scheduled task. Is there any merit using the backgroundworker thread for this? – w0051977 Nov 01 '13 at 12:14
  • I don't see any reason whatsoever why a BGW should not be used if your program gets started a different way. Using a form in a scheduled task, one that normally isn't visible, is however something you certainly could consider eliminating. – Hans Passant Nov 01 '13 at 12:21
  • @Hans Passant,thanks. I am using a main thread instead of Form_Load with application framework disabled in the project properties. Do you think a BGW is still needed? Thanks again. – w0051977 Nov 01 '13 at 12:44

2 Answers2

2

Sometimes this issue may also occur due to wrong server name, like if you had included SERVER/SQLEXPRESS instead of SERVER\SQLEXPRESS then also this error displays, which was my case.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

Check and be sure if you are using a reader that you do not included try/catches. You should make and effort to try and resolve your solution issue better than that. A try/catch will cause a time out especially in a while loop.

Nygons
  • 1