Jump to content

Converting a GUISetOnEvent to GUIGetMsg


Recommended Posts

I am converting a GUI using GUISetOnEvent to call functions over to a GUI that will use GUIGetMsg to perform actions in a switch case scenario. Here is the code:

Func ping()
    $ip_address = _runstdoutread("ipconfig")
    $ip_address = StringRegExpReplace($ip_address, "(?i)(?s).*IP.*?(\d+\.\d+\.\d+\.\d+).*$", "\1")
    $ping_results = _runstdoutread("Ping " & $ip_address & " -n 4")
    If StringRegExp($ping_results, "(?i)\(0%.*?\)") Then
        MsgBox(64, "Results", $ping_results)
        Return
    Else
        $hopen_write = FileOpen("error.txt", 2)
        FileWriteLine($hopen_write, StringFormat("IP: %s is down at %s", $ip_address, @HOUR & ":" & @MIN & ":" & @SEC))
        FileClose($hopen_write)
    EndIf
EndFunc

Func _runstdoutread($sruncmd)
    Local $ipid = Run(@ComSpec & " /c " & $sruncmd, @ScriptDir, @SW_HIDE, 4 + 2)
    Local $sstdoutread = ""
    While ProcessExists($ipid)
        $sstdoutread &= StdoutRead($ipid)
    WEnd
    Return $sstdoutread
EndFunc

When I add the Ping func data to a case and add the _runstdoutread as its own function I get an error regarding the 'return' in the first If statement under ping stating: 

"error: 'Return' not allowed from global scope."

My code so far:

While 1        
      Switch GUIGetMsg()
            Case $Ping
                $ip_address = _runstdoutread("ipconfig")
                $ip_address = StringRegExpReplace($ip_address, "(?i)(?s).*IP.*?(\d+\.\d+\.\d+\.\d+).*$", "\1")
                $ping_results = _runstdoutread("Ping " & $ip_address & " -n 4")
                    If StringRegExp($ping_results, "(?i)\(0%.*?\)") Then
                        MsgBox(64, "Results", $ping_results)
                        Return; <--- This is where I am getting the error
                    Else
                        $hopen_write = FileOpen("error.txt", 2)
                        FileWriteLine($hopen_write, StringFormat("IP: %s is down at %s", $ip_address, @HOUR & ":" & @MIN & ":" & @SEC))
                        FileClose($hopen_write)
                    
                    EndIf
WEnd

Func _runstdoutread($sruncmd)
    Local $ipid = Run(@ComSpec & " /c " & $sruncmd, @ScriptDir, @SW_HIDE, 4 + 2)
    Local $sstdoutread = ""
    While ProcessExists($ipid)
        $sstdoutread &= StdoutRead($ipid)
    WEnd
    Return $sstdoutread
EndFunc
EndFunc

If I don't have the return then I don't get statistics for my ping results (ie Approximate round trip times in Milli-seconds: Minimum = 118ms,Maximum = 120ms, Average = 118ms)

 

 

 

 

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

computergroove,

Instead of trying to move your function to the main msg loop just call your function, like this...

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <array.au3>

#AutoIt3Wrapper_Add_Constants=n

Local $gui010 = GUICreate('')
Local $aSize = WinGetClientSize($gui010)
Local $lbl010 = GUICtrlCreateLabel('', 0, 20, $aSize[0], 150, $ss_sunken)
Local $ping = GUICtrlCreatebutton('Ping', 20, $aSize[1]-30, $aSize[0]-40, 20)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $ping
            _ping()
    EndSwitch
WEnd

Func _ping()
    $ip_address = _runstdoutread("ipconfig")
    $ip_address = StringRegExpReplace($ip_address, "(?i)(?s).*IP.*?(\d+\.\d+\.\d+\.\d+).*$", "\1")
    $ping_results = _runstdoutread("Ping " & $ip_address & " -n 4")
    If StringRegExp($ping_results, "(?i)\(0%.*?\)") Then
        ;MsgBox(64, "Results", $ping_results)
        GUICtrlSetData($lbl010, "Results = " & $ping_results)
        Return
    Else
        $hopen_write = FileOpen("error.txt", 2)
        FileWriteLine($hopen_write, StringFormat("IP: %s is down at %s", $ip_address, @HOUR & ":" & @MIN & ":" & @SEC))
        FileClose($hopen_write)
    EndIf
EndFunc   ;==>_ping

Func _runstdoutread($sruncmd)
    Local $ipid = Run(@ComSpec & " /c " & $sruncmd, @ScriptDir, @SW_HIDE, 4 + 2)
    Local $sstdoutread = ""
    While ProcessExists($ipid)
        $sstdoutread &= StdoutRead($ipid)
    WEnd
    Return $sstdoutread
EndFunc   ;==>_runstdoutread

kylomas

edit: Note - I changed the function name from "ping" to _ping".

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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