3

I am running something similar to the following code in debug mode:

for (int i = 0; i < 5000; i++)
{
   for (int j = 0; j < 10; j++)
   {
     DoSomething();
   }
}

From time to time I am getting "ContextSwitchDeadlock was detected" from VS 2010. It seems that performance degrades with less RAM available. I have 8GB and able to run the program in around 8 minutes (there are LOTS of DB hits after the loops are done, so 8 minutes is not the loop time), on 4GB machines the time is doubled! From what I red,The MDA is fired because my loop runs for longer than 60 seconds. Essentially, it is a warning that a code is completely blocking the application.

What can I do to avoid these situations and improve speed?

Thanks!

payo
  • 4,501
  • 1
  • 24
  • 32
Eu Lupu
  • 361
  • 1
  • 4
  • 10
  • In order to comment on improving the speed we would need to see code. – Nanhydrin Mar 01 '12 at 17:11
  • The code has more than # of chars allowed, so I am not sure how can I post it. – Eu Lupu Mar 01 '12 at 19:31
  • I meant more the code that's actually inside "DoSomething()", there's not much you can do to speed up a basic for loop, it's what's being done inside the loop that's taking the time. That link that Payo provided below is good advice, that this work should be done in a separate backgroundworker thread. – Nanhydrin Mar 02 '12 at 09:39

2 Answers2

7

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
payo
  • 4,501
  • 1
  • 24
  • 32
  • 2
    Question is SHOULD I? Is this a REAL situation that slows down or eat memory or is just an exception that could be ignored? How can I determine where is my problem? – Eu Lupu Mar 01 '12 at 19:19
  • This is just visual studio warning you that a method is taking a very long time. It can be ignored - ignoring this exception will not affect your program, it only affects your debug experience. If your application is truly hung, you'll need to fix that. If, on the other hand, you expect your application to run for a very long time without hitting some type of 'context switch' (e.g. console read/writes, UI updates), then you'll want to ignore this exception. – payo Mar 01 '12 at 19:58
  • Also, this question was answered with more verbosity than I have given, http://stackoverflow.com/questions/2797677/contextswitchdeadlock-was-detected-error-in-c-sharp – payo Mar 01 '12 at 20:04
  • 1
    is this the answer!?! I'm new to stackoverflow and an upvote and answer-accepted would give me some newbie points :) – payo Mar 01 '12 at 23:00
  • I have a standalone exe with no UI, this is the answer for mine issue. – Chizl Dec 03 '13 at 14:28
0

If you are worried about speed and performance, why not used the IEnumerator Interface.

Botonomous
  • 1,746
  • 1
  • 16
  • 39
  • Is there a real advantage using the IEnumerator vs. foreach when you traverse a big collection? Is the call to Dispose() in foreach? I will try it and see if there is any difference. Thanks. – Eu Lupu Mar 01 '12 at 18:38