3

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!

Dilshod
  • 3,189
  • 3
  • 36
  • 67

2 Answers2

1

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.

Dilshod
  • 3,189
  • 3
  • 36
  • 67
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • 1
    Thanks for suggestion (break) I fixed it. As i told programm is running now. Stopping the debugger and changing the code will cause me lots of problems. That's why I am asking if I can stop it from Watch or Immediate Window. – Dilshod Sep 05 '13 at 03:17
  • 1
    Since you cannot control the program anyway, why wouldn't you want to stop it and fix the problem in your logic? – Karl Anderson Sep 05 '13 at 03:38
  • 1
    If I can break the loop then the rest of the code will finish the work. Then that doesn't cause me much more work. – Dilshod Sep 05 '13 at 03:44
  • 3
    If you are in the debugger, then put a break point anywhere in the loop, when the break point is hit, then drag the highlighted line down to the first line below the loop and hit F5 to continue on. – Karl Anderson Sep 05 '13 at 03:46
  • 1
    I tried that already, I am able to move it up or anywhere inside the loop, but can't drag it down. – Dilshod Sep 05 '13 at 03:49
0

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);
 }
Chris
  • 968
  • 16
  • 27
  • Thanks for the answer, but in my case it can't do anything. See my example. It was running for a long time and couldn't stop it at that time and do something like your example. – Dilshod Nov 14 '13 at 00:08
  • I was not able to use break from the immediate window either, for some reason it is ignored when used in the immediate at runtime..but when changing the value of the condition in a loop, then it breaks out without needing to use 'break', I just tested this again and had no trouble. edit: i meant you dont change the condition of the loop, you just change a value which causes the condition to no longer evaluate to true.. – Chris Nov 16 '13 at 20:29
  • I understand what you trying to explain. Did you see my example? I am statically using keyword "true", it is not a variable. is there way to change that true to false on runtime? I could just stop my program and change that as you suggested and break it any time I want, but I didn't wanted to do that. – Dilshod Nov 18 '13 at 12:55
  • No, you cannot change the value of the keyword 'true' at runtime. – Chris Nov 18 '13 at 16:41