Jump to content

How to loop function


Recommended Posts

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#NoTrayIcon

#Region ### START Koda GUI section ### Form=
$Main = GUICreate("Main", 197, 101, -1, -1)
$IP = GUICtrlCreateInput("210.5.78.4", 8, 8, 121, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_WANTRETURN))
GUICtrlSetLimit(-1, 20)
$Group1 = GUICtrlCreateGroup("", 8, 26, 180, 65, $BS_FLAT)
$Label1 = GUICtrlCreateLabel("Status:", 16, 42, 37, 17)
$Label2 = GUICtrlCreateLabel("Time:", 16, 65, 30, 17)
$Status = GUICtrlCreateLabel("READY", 56, 40, 121, 17, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetBkColor(-1, 0x008000)
$Pings = GUICtrlCreateLabel("0000", 56, 64, 36, 17, BitOR($SS_CENTER,$SS_CENTERIMAGE,$SS_SUNKEN))
$AutoRefresh = GUICtrlCreateCheckbox("AutoRefresh", 98, 64, 81, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Ping = GUICtrlCreateButton("Ping", 131, 8, 57, 20, $BS_FLAT)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Ping
            Call("_ping")
    EndSwitch
WEnd

Func _ping()
    $PingIP = GUICtrlRead($IP)
    $var = Ping($PingIP, 250)
If @error = 0 Then
    GUICtrlSetData($Status, "ONLINE")
    GUICtrlSetColor($Status, 0x000000)
    GUICtrlSetBkColor($Status, 0xFFFF00)
    GUICtrlSetFont($Status, 8, 800, 0, "MS Sans Serif")
    GUICtrlSetData($Pings, $var)
Else
    if @error = 1 Then
        GUICtrlSetData($Status, "OFFLINE")
        GUICtrlSetBkColor($Status, 0xFF0000)
        GUICtrlSetColor($Status, 0xFFFFFF)
        GUICtrlSetData($Pings, "----")
    EndIf
    if @error = 2 Then
        GUICtrlSetData($Status, "Host Unreachable")
        GUICtrlSetBkColor($Status, 0xFF0000)
        GUICtrlSetColor($Status, 0xFFFFFF)
        GUICtrlSetData($Pings, "----")
    EndIf
    if @error = 3 Then
        GUICtrlSetData($Status, "Bad Destination")
        GUICtrlSetBkColor($Status, 0xFF0000)
        GUICtrlSetColor($Status, 0xFFFFFF)
        GUICtrlSetData($Pings, "----")
    EndIf
    if @error = 4 Then
        GUICtrlSetData($Status, "Unknown Error")
        GUICtrlSetBkColor($Status, 0xFF0000)
        GUICtrlSetColor($Status, 0xFFFFFF)
        GUICtrlSetData($Pings, "----")
    EndIf       
EndIf
EndFunc

Could anyone help me to loop the Function "_Ping()" when i press "Ping" then will stop when i press "Ping" again.

Link to comment
Share on other sites

Change the main loop to something like this:

$fPinging = False

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Ping
            $fPinging = Not $fPinging
    EndSwitch
    If $fPinging Then Call("_ping")
    Sleep(100)
WEnd

You might also want to change the Sleep() to a higher number.

WBD

Link to comment
Share on other sites

I know that but I was just keeping the original code; didn't want to cause confusion ^_^

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...