title: Pause While loop by hotkey
body: <p>I want to pause an AutoIt script containing a <code>While</code> loop and some functions. But I am only able to close the script on <code>HotKeySet()</code>. How can I pause it?</p>
  
  <p>The script checks for changes on a part of screen (x,y coordinates are set in a config file) and takes screenshots after playing an alert sound. It doesn't stop the <code>While</code> loop when pushing the pause button. But closing the program works. Here is my code:</p>
  
  <pre><code>Global $Paused, $counter = 0
  HotKeySet("{1}", "TogglePause")
  HotKeySet("{2}", "Terminate")
  HotKeySet("{3}", "ShowMessage")    
  
  Init()
  Start()
  While 1
     $counter +=1
      ToolTip('Script is "Running"',0,0, $counter, 1)
      Sleep(700)
        Switch TrayGetMsg()
        Case $resume
        Start()
        DisableAlert()
        Case $exit
        ExitLoop
        Exit
      EndSwitch
   WEnd    
  
  //some of the functions    
  Func Start()
      $ready = 0
      $count = 0
      $lastScreenshotNum = 0
      TrayItemSetState($resume, $TRAY_DISABLE)
      TraySetIcon("on.ico")
      TakeScreenshot()
      AdlibRegister(TakeScreenshot,2000)
  EndFunc    
  
  Func Stop()
      AdlibUnRegister(TakeScreenshot)
      TraySetIcon("off.ico")
      TrayItemSetState($resume, $TRAY_ENABLE)
  EndFunc
  
  Func TogglePause()
     Stop()
      $Paused = NOT $Paused
      While $Paused
          sleep(100)
          ToolTip('Script is "Paused"',0,0, $counter, 1)
      WEnd
      ToolTip("")
  EndFunc
  
  Func Terminate()
      Exit 0
  EndFunc
  
  Func ShowMessage()
      MsgBox(4096,"","This is a message.")
  EndFunc
  
  Func EnableAlert()
      SendMail()
      Alert()
      AdlibRegister(Alert,5000)
  EndFunc
  
  Func DisableAlert()
      AdlibUnRegister(Alert)
  EndFunc
  
  Func Alert()
      SoundPlay("alert.mp3")
  EndFunc
  </code></pre>