Breaking the endless loop from watch or immediate window

1.5k views Asked by At

I wrote app that uses while loop. And in my code I forgot to put break in a specific condition. I am running the application from the debugger. Now I need to quit from the loop. Some people could say just stop it and put that break line in, but I can't at this point. Changing the code and running again takes long time. Is there way to stop this from the watch window or Immediate Window?

Here is example of loop that I have:

while(true)
{
    //do the work
    if(something happens)
        break; //I missed this part!
}
//some other things

Thanks for the help!

2

There are 2 answers

5
Karl Anderson On

You should re-write your while loop to use a variable instead of true, then you can use the debugger to change the value of your variable to false and end the loop, like this:

bool doWork = true;

while(doWork)
{
    // Do looping logic here
}

Now in the debugger, you can break on your loop and update the doWork variable to false if you want to break out of the loop.

4
Chris On

Karl is correct.. Here is the same in a for loop with specific instructions..:

(In VS2010, you can just mouseover on isTrue, the rightclick on 'false' when you see it in the tooltip, then click edit value, and type in true instead of false)

 bool isTrue = true;
 for (; isTrue ; )
 {
      Thread.Sleep(1000);
 }