Breaking Out of Loops

QUESTION: Is there any way to break out of a loop when debuging in IDL? I've tried CTRL-C and CTRL-BREAK (which sometimes works) but not in this case. I'm running on Windows.

ANSWER: The answer comes from an interesting discussion on the IDL newsgroup, entitled Endless Loops Suck.

On non-Windows platforms, it turns out that loops can be interrupted with a CTRL-C if they are written with BEGIN - END statement blocks. For example, this loop is not interruptable on any IDL platform:

   PRO LoopIt
     a = 1
     WHILE 1 DO a = a + 1
   END

This loop would be interruptable on UNIX platforms with an CRTL-C by writing the loop like this:

   PRO LoopIt
      a = 1
      WHILE 1 DO BEGIN
         a = a + 1
      ENDWHILE
   END

The corresponding functionality to break into a loop on Windows is suppose to be CTRL-BREAK, but as you have discovered, sometimes this works and sometimes it doesn't. The newsgroup article tried to sort out the conditions necessary to get this to work, and I provide a brief synopsis of the discussion here.

In additon to BEGIN - END statement blocks, Windows requires that there be some kind of operating system access in the loop. For example, the loop above is not interruptable in Windows. But it could be made interruptable by adding a Print statement in the loop, like this:

   PRO LoopIt
      a = 1
      WHILE 1 DO BEGIN
         a = a + 1
         Print, a
      ENDWHILE
   END

You could break out of the loop above by using a CNTL-BREAK sequence of key strokes.

But the Print statement is computationally expensive, not to mention unsightly. What are some alternatives?

Several people pointed out that a very short Wait statement was effective:

   PRO LoopIt
      a = 1
      WHILE 1 DO BEGIN
         a = a + 1
         Wait, 0.005
      ENDWHILE
   END

But the prize was won by Altyntsev Dmitriy who discovered the effectiveness of the Widget_Event statement in creating interrubtable loops on Windows machines:

   PRO LoopIt
      a = 1
      WHILE 1 DO BEGIN
         a = a + 1
         tmp = Widget_Event(/NoWait)
      ENDWHILE
   END

Google
 
Web Coyote's Guide to IDL Programming