Sign in to follow this  
Followers 0
dash007

Loop Problem

9 posts in this topic

Hi,

Below is the script, I have used a script found in this forum and tried to build a GUI for it. However the buttons don't seem to be working in the second GUI Window "STDIO Window". Can somebody please assist me with this, its breaking my head.

#include <GUIConstants.au3>

$parent1 = GUICreate("AutoIT", 240, 125)
opt("GUIOnEventMode", 1)

$Note = GUICtrlCreateButton("Notepad", 10, 14, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($Note, "Note")
$sPing = GUICtrlCreateButton("Ping", 100, 14, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($sPing, "sPing")
$MExit = GUICtrlCreateButton("Exit", 50, 54, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($MExit, "MExit")
GUISetOnEvent($GUI_EVENT_CLOSE, "MExit")

GUISetState()

While 1
Sleep(100)
WEnd

Func Note()
Run("notepad.exe")
Endfunc

Func sPing()

Dim $ourProcess

GuiCreate("STDIO Window", 425, 322,(@DesktopWidth-425)/2, (@DesktopHeight-362)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$eOutput = GuiCtrlCreateEdit("", 10, 40, 350, 230, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY))

$PBtn = GUICtrlCreateButton("Ping Now", 25, 285, 80, 20)
$bExit = GuiCtrlCreateButton("Exit", 340, 285, 60, 20)

GuiSetState()
Endfunc

   $msg = GuiGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
         Exit
      Case $msg = $bExit
         Exit
      Case $msg = $Pbtn
    $ourProcess = Run("ping www.autoit.com", @SystemDir, @SW_HIDE, 2)
Endselect

While 1
   If $ourProcess Then

      $charsWaiting = StdoutRead($ourProcess, 0 , 1)
      If @error = -1 Then
         $ourProcess = 0
        ContinueLoop
      EndIf
      If $charsWaiting Then
         $currentRead = StdoutRead($ourProcess)
         GUICtrlSetData($eOutput, $currentRead, 1)
      EndIf
   EndIf
   $msg = GuiGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
         ExitLoop
      Case $msg = $bExit
         ExitLoop
      Case Else
      ;;;
   EndSelect
WEnd
Exit
;endif

If $msg = $GUI_EVENT_CLOSE Or $msg = $bExit Then Exit

;wend   

GUISetState()
                        
While 1
Sleep(100)
WEnd

;Endfunc

Func MExit()
    Exit
EndFunc

Share this post


Link to post
Share on other sites



$msg = GuiGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
         Exit
      Case $msg = $bExit
         Exit
      Case $msg = $Pbtn
    $ourProcess = Run("ping www.autoit.com", @SystemDir, @SW_HIDE, 2)
Endselect

needs to be in a while 1...wend loop, because a GUI message may not arrive at the exact second you ask for GUIGetMsg()


Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites

I have tried the below (with the suggestion from MSLx Fanboy) but it still does not work for me. See new code below:

#include <GUIConstants.au3>

$parent1 = GUICreate("AutoIT", 240, 125)
opt("GUIOnEventMode", 1)

$Note = GUICtrlCreateButton("Notepad", 10, 14, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($Note, "Note")
$sPing = GUICtrlCreateButton("Ping", 100, 14, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($sPing, "sPing")
$MExit = GUICtrlCreateButton("Exit", 50, 54, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($MExit, "MExit")
GUISetOnEvent($GUI_EVENT_CLOSE, "MExit")

GUISetState()

While 1
Sleep(100)
WEnd

Func Note()
Run("notepad.exe")
Endfunc

Func sPing()

Dim $ourProcess

GuiCreate("STDIO Window", 425, 322,(@DesktopWidth-425)/2, (@DesktopHeight-362)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$eOutput = GuiCtrlCreateEdit("", 10, 40, 350, 230, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY))

$PBtn = GUICtrlCreateButton("Ping Now", 25, 285, 80, 20)
$bExit = GuiCtrlCreateButton("Exit", 340, 285, 60, 20)

GuiSetState()
Endfunc

While 1
   $msg = GuiGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
         Exit
      Case $msg = $bExit
         Exit
      Case $msg = $Pbtn
    $ourProcess = Run("ping www.autoit.com", @SystemDir, @SW_HIDE, 2)
Endselect
Wend

While 1
   If $ourProcess Then

      $charsWaiting = StdoutRead($ourProcess, 0 , 1)
      If @error = -1 Then
         $ourProcess = 0
        ContinueLoop
      EndIf
      If $charsWaiting Then
         $currentRead = StdoutRead($ourProcess)
         GUICtrlSetData($eOutput, $currentRead, 1)
      EndIf
   EndIf
   $msg = GuiGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
         ExitLoop
      Case $msg = $bExit
         ExitLoop
      Case Else
     ;;;
   EndSelect
WEnd
Exit
;endif

If $msg = $GUI_EVENT_CLOSE Or $msg = $bExit Then Exit

;wend   

GUISetState()
                        
While 1
Sleep(100)
WEnd

;Endfunc

Func MExit()
    Exit
EndFunc

Perhaps I have misunderstood. Please help.

Share this post


Link to post
Share on other sites

Anyone else have any suggestions?

Share this post


Link to post
Share on other sites

Can somebody test the above code and advice if its working for them or not please..

Share this post


Link to post
Share on other sites

Yes, I sure can advise that it's not working. Now, can we go about looking at what's wrong with the script? We'll refer to the Help File from time-to-time...

First thing that catches my eye is lines 17-19:

While 1
Sleep(100)
WEnd

...which tells the script "Do nothing, forever, with .1 second pauses in between." Seeing this, I'm not surprised that the script doesn't behave as desired.

Ah, OK, then I see that the script was thrown into GUI event mode at the top and functions are paired with button events:

opt("GUIOnEventMode", 1)

$Note = GUICtrlCreateButton("Notepad", 10, 14, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($Note, "Note")
$sPing = GUICtrlCreateButton("Ping", 100, 14, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($sPing, "sPing")
$MExit = GUICtrlCreateButton("Exit", 50, 54, 80, 23, $BS_FLAT)
GUICtrlSetOnEvent($MExit, "MExit")
GUISetOnEvent($GUI_EVENT_CLOSE, "MExit")

...so AutoIt, during the time that it's idle in those .1 second Sleeps sees there's an event when you click a button and runs the function paired to that button.

OK, but I see another infinite while loop with more code in it below some function defs, so maybe we should comment the empty one above and use this one.

But wait, looking to the help file under GUI Concepts we see:

Well, this is where we must make a decision as to how we will process events - via a MessageLoop or via OnEvent functions.

Ah, we're in GUI Event mode, so the call to GuiGetMsg() in this new loop won't return anything, so none of the cases will ever be selected, so we've essentailly got an empty loop again...

Should I go on to the third infinite While loop? It at least would test the $ourProcess variable if we commented out the other 2 loops..? But no, $ourProcess is only meaningful in the scope of the sPing() function, so it can't ever be valid in this loop, so again we've just got an empty, infinite loop.

And with that, I'm out of time...

I'm sorry I'm being a bastard here, and I can tell that you're frustrated, but you couldn't really expect a mishmash of code from different posts to "just work" and you can't let your mind shut off just because something doesn't behave as expected...

Blah.


Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Share this post


Link to post
Share on other sites

@DaveF

I don't see why you have to be so sarcastic in answering my question. Obviously I have posted in the right forum "V3 support". I knew there were mistakes and hence required assistance.

We all have to start from somewhere, so be kind on people who don't have the understanding of AutoIt as you may have. MSLx Fanboy didn't pick up on the points you raised (I am not saying that MSLx Fanboy would ever do a bad coding as me), I am grateful to him for having taken the time to write a reply. I am only proving a point that we all make mistakes. :)

Share this post


Link to post
Share on other sites

You're correct, it was wrong of me to leave a nasty post on your topic, and I apologize.

Please find an example script below. I hope it is instructional.

; A script for AutoIt3. Jon is The Man.
; Script generated by AutoBuilder 0.5 Prototype. CyberSlug rocks.
; Edited with SciTE and Tidy. Go JdeB.

#include <GuiConstants.au3>

Dim $ourProcess

GuiCreate("STDIO Window", 425, 322,(@DesktopWidth-425)/2, (@DesktopHeight-362)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

; Create a read-only edit control
$eOutput = GuiCtrlCreateEdit("", 0, 10, 423, 260, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY))
; Ping spawn button
$bPing = GuiCtrlCreateButton("Ping", 180, 285, 60, 20)
; Notepad spawn button
$bNotepad = GuiCtrlCreateButton("Notepad", 260, 285, 60, 20)
; Exit button
$bExit = GuiCtrlCreateButton("Exit", 340, 285, 60, 20)

; Show the GUI window
GuiSetState()

; Loop and update the edit control unitl we hit EOF (and get an error)
While 1
  ; We assign the process ID of ping to $ourProcess below...
   If $ourProcess Then
     ; Calling StdoutRead like this returns the characters waiting to be read
      $charsWaiting = StdoutRead($ourProcess, 0 , 1)
     ; If there was an error reading, the most likely cause us that the child process has quit...
      If @error = -1 Then
        ; Set $ourProcess to zero so we don't try to read nothing...
         $ourProcess = 0
         MsgBox(0, "App Exited", "Process has exited...")
        ; ContinueLoop means "don't exit the While loop, just start over without doing anything else"
         ContinueLoop
      EndIf
     ; Since we got here there was no error, but were there characters to be read?
      If $charsWaiting Then
        ; Read all available
         $currentRead = StdoutRead($ourProcess)
        ; Add what we read to the editbox
         GUICtrlSetData($eOutput, $currentRead, 1)
      EndIf
   EndIf
  ; Get any messages from the GUI
   $msg = GuiGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
        ; X button on window was clicked
         ExitLoop
      Case $msg = $bExit
        ; Exit button was clicked
         ExitLoop
      Case $msg = $bPing
        ; Ping button was clicked
        ; Run child process and provide console i/o for it.
        ; Parameter of 2 = provide standard output
         $ourProcess = Run("ping autoitscript.com", @SystemDir, @SW_HIDE, 2)
      Case $msg = $bNotepad
        ; Notepad button was clicked
         Run("notepad.exe", @SystemDir)
      Case Else
        ;;;
   EndSelect
WEnd
Exit

Cheers.


Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Share this post


Link to post
Share on other sites

DaveF,

Thank you for helping me. You have detailed every step which is very nice of you. It will certainly be helpful in my future coding too. :)

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