Jump to content

While-Loop blocks other Actions


Recommended Posts

i wrote a small script which retrievs news from a awebpage

i made a whileloop for that my problem is while that whileloop is running i cant press any buttons in the script.

i bound a hotkey to exit the whileloop but i want a button for that how can i rewrite the function so it doesnt blocks any controlinput i make

Func getmsgs()
    Global $nExit = 1
    Global $php_id = IniRead($script[1] & ".ini", "User", "username", "none")
    Global $pass = IniRead($script[1] & ".ini", "User", "userpass", "none")
    _GUICtrlEdit_InsertText($Edit1, "Press Escape to cancel Action before Closing Program or changeing settings" & @CRLF & @CRLF, -1)
    While $nExit
        $time = @HOUR & ":" & @MIN & ":" & @SEC
        $date = @MDAY & "." & $mon & "." & @YEAR
        InetGet($url & "getid.php?pass=" & $pass & "&user_id=" & $php_id, $textfile, 1, 0)
        $file = FileOpen($textfile, 0)
        $id = FileReadLine($file, 1)
        FileClose($file)
        FileDelete($textfile)
        If (StringIsInt($id)) Then
            InetGet($url & "unset.php?user_id=" & $php_id & "&id=" & $id & "&stat=read", $textfile, 1, 0)
            $file = FileOpen($textfile, 0)
            $au3 = FileReadLine($file, 1)
            FileClose($file)
            FileDelete($textfile)
            If ($au3 == "somefuncname") Then
                MsgBox(0, $Windowname, "put a function here", 5)
            Else
                _GUICtrlEdit_InsertText($Edit1, "=== New message recieved ===== Time : " & $time & " = Date : " & $date & " =====" & @CRLF & _
                        "Message : " & $au3 & @CRLF & _
                        "=============================================== End of Message ===" & @CRLF & @CRLF, -1)
            EndIf
            Sleep(3000)
            InetGet($url & "unset.php?user_id=" & $php_id & "&id=" & $id & "&stat=upd", $textfile, 1, 0)
            FileDelete($textfile)
        Else
            Sleep(6000)
        EndIf
    WEnd
    _GUICtrlEdit_InsertText($Edit1, "Message refresh stopped" & @CRLF, -1)
EndFunc ;==>getmsgs
Edited by unixu
Link to comment
Share on other sites

What does the HotKey do? Set $nExit to 0? If so, you're script has a minimum sleep time of like 9 seconds, plus whatever functions in there will hold up the While loop. The loop needs to go thru everything until it checks $nExit again. You may want to check for other things inside the while loop and use ExitLoop to get out of it. Are you sure your hot key doesn't work? Or maybe it does but there's just a delay?

Edited by MrMitchell
Link to comment
Share on other sites

i wrote a small script which retrievs news from a awebpage

i made a whileloop for that my problem is while that whileloop is running i cant press any buttons in the script.

i bound a hotkey to exit the whileloop but i want a button for that how can i rewrite the function so it doesnt blocks any controlinput i make

Func getmsgs()
      Global $nExit = 1
      Global $php_id = IniRead($script[1] & ".ini", "User", "username", "none")
      Global $pass = IniRead($script[1] & ".ini", "User", "userpass", "none")
      _GUICtrlEdit_InsertText($Edit1, "Press Escape to cancel Action before Closing Program or changeing settings" & @CRLF & @CRLF, -1)
      While $nExit
          $time = @HOUR & ":" & @MIN & ":" & @SEC
          $date = @MDAY & "." & $mon & "." & @YEAR
          InetGet($url & "getid.php?pass=" & $pass & "&user_id=" & $php_id, $textfile, 1, 0)
          $file = FileOpen($textfile, 0)
          $id = FileReadLine($file, 1)
          FileClose($file)
          FileDelete($textfile)
          If (StringIsInt($id)) Then
              InetGet($url & "unset.php?user_id=" & $php_id & "&id=" & $id & "&stat=read", $textfile, 1, 0)
              $file = FileOpen($textfile, 0)
              $au3 = FileReadLine($file, 1)
              FileClose($file)
              FileDelete($textfile)
              If ($au3 == "somefuncname") Then
                  MsgBox(0, $Windowname, "put a function here", 5)
              Else
                  _GUICtrlEdit_InsertText($Edit1, "=== New message recieved ===== Time : " & $time & " = Date : " & $date & " =====" & @CRLF & _
                          "Message : " & $au3 & @CRLF & _
                          "=============================================== End of Message ===" & @CRLF & @CRLF, -1)
              EndIf
              Sleep(3000)
              InetGet($url & "unset.php?user_id=" & $php_id & "&id=" & $id & "&stat=upd", $textfile, 1, 0)
              FileDelete($textfile)
          Else
              Sleep(6000)
          EndIf
      WEnd
      _GUICtrlEdit_InsertText($Edit1, "Message refresh stopped" & @CRLF, -1)
  EndFunc;==>getmsgs
I have to guess because you only provided the function but I assume that you're not using OnEvent mode.

All you need to do (I think) is add something like the following inside your while loop

While $nExit
 $msg = GuiGetMsg()
 switch $msg
    Case $Button1
      ;react to button1
    Case $button2
      ;react to button2 etc
    EndSwitch
 
;rest of the existing code in your while loop
 
 Wend

Edit: Oh I forgot, as MrMitchell says, you will need to get rid of the sleeps or reduce them a lot if you want to respond to the buttons.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Edit: Oh I forgot, as MrMitchell says, you will need to get rid of the sleeps or reduce them a lot if you want to respond to the buttons.

the problem i got a very slow webserver ... so i need at least 10 seconds before i can send the next command to it.....

the sleep(6000) is only the check interval so the scripot checks the webpage every6 seconds for new messages.

the sleep(3000) is for my own mysql database so the script knows which news are checked and which not

i tried your solution with the case msg thing id didnt worked.

so i took the whileloop and paste it out of the function and it works now the whileloop is running and i can still work with the gui thx anyway for your help :)

Link to comment
Share on other sites

I usualy use a separate function to handle GUI and call that function every 10ms or so:

AdlibEnable("guiloop", 10)

It works when that main script is doing with something else on the side.

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