Sign in to follow this  
Followers 0
computergroove

Converting a GUISetOnEvent to GUIGetMsg

5 posts in this topic

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

Share this post


Link to post
Share on other sites



Remove this Return  :)

Share this post


Link to post
Share on other sites

Return is used for giving back data back to the script after a function has ended.

You can't just use return outside of a function, that would make no sense.

Also:

- There seems to be no EndSwitch in your code

- There's 2 x EndFunc at the end.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Anyway,

Func _ping()
    ;some code
    If <condition> Then
        ;do something
        Return    ;<<<<<<<<<< totally useless, the Else part doesn't execute then you go straight from here to the end of the func
    Else
        ;do something else
    EndIf
EndFunc

Share this post


Link to post
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
Sign in to follow this  
Followers 0