Jump to content

While and GUI_EVENT_CLOSE


Recommended Posts

Hi All,

I know this has been asked a few times but I need help with the general idea with While loops, I have tried GUIOnEvent and AdlibRegister but I can't seem to get it to work, ive added all my code (hopefully it will syntax), I have added a comment where I think the code would go but not sure

Thanks Daniel

--

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstants.au3>
#include <MsgBoxConstants.au3>

$Title = "Back Online? v1 by Dan"

HotKeySet("{ESC}", "_Terminate")

GUICreate($Title,300,200)
$sCenter = WinGetClientSize($Title)
GUICtrlCreateLabel("Back Online?",0,10, $sCenter[0],50,$SS_CENTER)
GUICtrlSetFont (-1,20)

GUICtrlCreateLabel("Enter The IP Address To Monitor", 0, 60,  $sCenter[0],-1,$SS_CENTER)
GUICtrlSetFont (-1,12)
Global $sIPAddr = GUICtrlCreateInput("",70, 90, $sCenter[0]/2,30,$ES_CENTER)
$sStartPing = GUICtrlCreateButton("Monitor",70,125,$sCenter[0] / 2,25,$SS_CENTER)
GUICtrlCreateLabel("Waiting IP...", 120, 175,60)

GUISetState(@SW_SHOW)

$counter = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $sStartPing
            If GUICtrlRead($sIPAddr) == "" Then
               MsgBox(64,$Title,"Yeet says hi!")
            Else
            _Ping(GUICtrlRead($sIPAddr))
         EndIf

   EndSwitch
WEnd

Func _Ping($ipaddr)
    While 1
      If Ping($ipaddr, 50) Then
          _Success();$ipaddr)
       Else
          $counter +=1
          if $counter > 10 Then
             MsgBox(64,$Title,"I dont think its coming back?"&@CRLF&"Ill Close")
             ; use ie_create http://localhost/backon.php?status=Offline&ip=$ipaddr
             Exit
          EndIf
          GUICtrlSetData(-1,"Attempt "&$counter)
       EndIf

;;;; Where I suspect the GUI_CLOSE may occur? 

       Sleep(200)
    WEnd
EndFunc

Func _Terminate()
    Exit
EndFunc

Func _Success()
  MsgBox(64,$Title,"Its back up,"&@CRLF&"Ive sent you a notification")
; use ie_create http://localhost/backon.php?status=Backup&ip=$ipaddr

   Exit
EndFunc

 

Link to comment
Share on other sites

When you go into your _Ping function it's stuck in the While loop. If you want to have the gui close, you need to add in code to read the GUIGetMsg again and action on that or have some way to exit the loop to return to the main message loop.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You might like to use 

Opt("GUIOnEventMode", 1)
GUICtrlSetOnEvent .....

to trap your GUI events and run functions when they are clicked. This frees you from constantly checking for them in a while / case loop. There are some good examples on the GUICtrlSetOnEvent help page. I'm sure there are appropriate use cases for both methods, but I find I always use this method to write GUIs in AutoIt.

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

Alternatively use AdlibRegister for example:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstants.au3>
#include <MsgBoxConstants.au3>

Global $bPing = False, $iCounter = 0, $Title = "Back Online? v1 by Dan"

HotKeySet("{ESC}", "_Terminate")

GUICreate($Title,300,200)
$sCenter = WinGetClientSize($Title)
GUICtrlCreateLabel("Back Online?",0,10, $sCenter[0],50,$SS_CENTER)
GUICtrlSetFont (-1,20)

GUICtrlCreateLabel("Enter The IP Address To Monitor", 0, 60,  $sCenter[0],-1,$SS_CENTER)
GUICtrlSetFont (-1,12)
Global $sIPAddr = GUICtrlCreateInput("",70, 90, $sCenter[0]/2,30,$ES_CENTER)
$sStartPing = GUICtrlCreateButton("Monitor",70,125,$sCenter[0] / 2,25,$SS_CENTER)
GUICtrlCreateLabel("Waiting IP...", 120, 175,60)

GUISetState(@SW_SHOW)

AdlibRegister("_Ping")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $sStartPing
            If GUICtrlRead($sIPAddr) == "" Then
                MsgBox(64,$Title,"Yeet says hi!")
                GUICtrlSetData($sStartPing, "Monitor")
                $bPing = False
            Else
                GUICtrlSetData($sStartPing, "Monitoring...")
                $bPing = True
            EndIf
   EndSwitch
WEnd

Func _Ping()
    If $bPing = False Then Return
    If Ping(GUICtrlRead($sIPAddr), 50) Then
        _Success();$ipaddr)
    Else
        $iCounter +=1
        if $iCounter > 10 Then
            MsgBox(64,$Title,"I dont think its coming back?"&@CRLF&"Ill Close")
            ; use ie_create http://localhost/backon.php?status=Offline&ip=$ipaddr
            Exit
        EndIf
            GUICtrlSetData(-1,"Attempt "&$iCounter)
       EndIf
EndFunc

Func _Terminate()
    Exit
EndFunc

Func _Success()
  MsgBox(64,$Title,"Its back up,"&@CRLF&"Ive sent you a notification")
; use ie_create http://localhost/backon.php?status=Backup&ip=$ipaddr

   Exit
EndFunc

 

Link to comment
Share on other sites

Hi All,

Thanks for your replies, after trying each and more Binging I found this article https://www.autoitscript.com/wiki/Interrupting_a_running_function

It uses a flag var and GUIOnEventMode

It also is using the Google Dinosaur as its icon

There is the complete code

Thanks again

Daniel

;https://www.autoitscript.com/forum/topic/198689-while-and-gui_event_close/
;https://www.autoitscript.com/wiki/Interrupting_a_running_function

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstants.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>

Global $fRunTime = False, $iCounter = 0, $Title = "Back Online? v1.3 by Dan"

HotKeySet("{ESC}", "_Exit")
Opt("GUIOnEventMode", 1)

GUICreate($Title,300,200)
GUISetIcon("dino.ico")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$sCenter = WinGetClientSize($Title)
GUICtrlCreateLabel("Back Online?",0,10, $sCenter[0],50,$SS_CENTER)
GUICtrlSetFont (-1,20)

GUICtrlCreateLabel("Enter The IP Address To Monitor", 0, 60,  $sCenter[0],-1,$SS_CENTER)
GUICtrlSetFont (-1,12)
Global $sIPAddr = GUICtrlCreateInput("",70, 90, $sCenter[0]/2,30,$ES_CENTER)
$sStartPing = GUICtrlCreateButton("Monitor",70,125,$sCenter[0] / 2,25,$SS_CENTER)
GUICtrlSetOnEvent(-1, "_Ping_Run")
$idCheckbox = GUICtrlCreateCheckbox("Alert?",125,155)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlCreateLabel("Waiting IP...", 120, 180,60)

GUISetState()

While 1
   Sleep(10)
   If $fRunTime Then
         ; Now start the "real" function from within the main code
         _Ping()
     EndIf
WEnd

Func _Ping_Run()
     ; Set the flag within the OnEvent function
     $fRunTime = True
 EndFunc

Func _Ping()
   $ipaddr = GUICtrlRead($sIPAddr)
   if $ipaddr == "" Then
      Msgbox(64,$Title,"No chance")
      $fRunTime = False
      Return
   EndIf

    While 1
      If Ping($ipaddr, 50) Then
          _Success();$ipaddr)
       Else
          $iCounter +=1
          if $iCounter > 1000 Then
            Local $oIE = _IECreate("http://localhost/u/bon/?status=Offline",0,0)
            _IEQuit($oIE)
            If _IsChecked($idCheckbox) Then
               MsgBox(64,$Title,"I dont think its coming back?"&@CRLF&"Ill Close")
            EndIf
             Exit
          EndIf
          GUICtrlSetData(-1,"Attempt "&$iCounter)
       EndIf
    WEnd
EndFunc

Func _Exit()
    Exit
EndFunc

Func _Success()
   Local $oIE = _IECreate("http://localhost/u/bon/?status=Online",0,0)
   _IEQuit($oIE)
   If _IsChecked($idCheckbox) Then
   MsgBox(64,$Title,"Its back up,"&@CRLF&"Ive sent you a notification")
   EndIf
   Exit
EndFunc

Func _IsChecked(Const $iControlID)
   Return BitAND(GUICtrlRead($iControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...