Jump to content
Sign in to follow this  
mrflibblehat

While Loop Stopping GuiOnEventMode Actions

Recommended Posts

mrflibblehat

Hi All,

I am developing an IRC Bot and have come across a small problem. Everything works fine until the ConnectToIRC function is called. This is when a while Loop is in play, I assume this is because the while loop is hogging the program and blocking other commands.

so my question, Is there a way around this or a way I could do this differently so that I can receive data when it comes in and also use controls on the gui?

If I remove the while loop inside the ConnectToIRC function, Other gui buttons work correctly (Close Button for example)

Thanks for any help or insight you may provide.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GUIEdit.au3>
#Include <ScrollBarConstants.au3>
#include <ComboConstants.au3>
#Include <Array.au3>

Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("AutoIT IRCBot", 537, 339, 192, 124)
$Edit1 = GUICtrlCreateEdit("", 8, 8, 521, 257, $ES_AUTOVSCROLL + $WS_VSCROLL)
$btnSend = GUICtrlCreateButton("Send", 456, 304, 73, 25)
$inIRCServ = GUICtrlCreateInput("irc.freenode.net", 8, 272, 97, 21)
$inPort = GUICtrlCreateInput("6667", 112, 272, 65, 21)
$inChat = GUICtrlCreateInput("", 288, 304, 161, 21)
$btnCon = GUICtrlCreateButton("Connect", 184, 272, 73, 25)
$Combo1 = GUICtrlCreateCombo("Channel", 288, 272, 161, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseBot")
GUICtrlSetOnEvent($btnCon, "ConnectToIRC")
GUICtrlSetOnEvent($btnSend, "IRCSend")
GUISetState(@SW_SHOW)

TCPStartup()

;~ Read INI Values
Global $vIRCNick = IniRead("ircbot.ini", "information", "Nick", "AutoITIRCBot")
Global $vIRCAltNick = IniRead("ircbot.ini", "information", "AltNick", "AutoITIRCBot_")
Global $vIRCRealName = IniRead("ircbot.ini", "information", "RealName", "AutoITIRCBot")
Global $vIRCChannels = IniRead("ircbot.ini", "information", "Channels", "#AutoITIRCBot")

$vChanArr = StringSplit($vIRCChannels, "|")

While 1
    Sleep(100)
WEnd

Func ConnectToIRC()
    GUICtrlSetState($btnCon, $GUI_DISABLE)
    GUICtrlSetState($inIRCServ, $GUI_DISABLE)
    GUICtrlSetState($inPort, $GUI_DISABLE)
    
    $vIRCServer = TCPNameToIP(GuiCtrlRead($inIRCServ))
    $vIRCPort = GuiCtrlRead($inPort)
    $vConnect = TCPConnect($vIRCServer, $vIRCPort)

    TCPSend($vConnect, "USER " & $vIRCNick & " 0 0 :" & $vIRCRealName & @CRLF)
    TCPSend($vConnect, "NICK "  & $vIRCNick & @CRLF)

    For $i = 1 to Ubound($vChanArr) -1
        TCPSend($vConnect, "JOIN " & $vChanArr[$i] & @CRLF)
        GUICtrlSetData($Combo1, $vChanArr[$i], $vChanArr[$i])
    Next

    While 1
        $vRecv = TCPRecv($vConnect, 4096)
        If $vRecv <> "" Then
            GuiCtrlSetData($Edit1, $vRecv, 1)
        EndIf
    Wend
EndFunc

Func CloseBot()
    Exit
EndFunc

Ini File

[information]
Nick=UKTestBot
AltNick=UKTestBot_
RealName=UKTestBOT
Channels=#test333|#test222
Edited by mrflibblehat

[font="'courier new', courier, monospace;"]Pastebin UDF | Prowl UDF[/font]

Share this post


Link to post
Share on other sites
Melba23

mrflibblehat,

 

I assume this is because the while loop is hogging the program and blocking other commands

Correct, AutoIt will queue any other commands until you return to the main idle loop. :)

I suggest you move the contents of that While..WEnd loop inside your main idle loop and use a flag to tell it when to operate - something like this (untested):

; Add this at the start
Global $fJoin = False

; [other code]

While 1
    Sleep(100)
    
    ; Look for joins if needed
    If $fJoin Then
        $vRecv = TCPRecv($vConnect, 4096)
        If $vRecv <> "" Then
            GuiCtrlSetData($Edit1, $vRecv, 1)
        EndIf
    EndIf
    
WEnd

Func ConnectToIRC()
    
    ; [other code]
    
    For $i = 1 to Ubound($vChanArr) -1
        TCPSend($vConnect, "JOIN " & $vChanArr[$i] & @CRLF)
        GUICtrlSetData($Combo1, $vChanArr[$i], $vChanArr[$i])
    Next

    ; Set the flag
    $fJoin = True
    
EndFunc
You might also think of clearing the flag at some point by looking to see if you have received the correct number of responses so that you only fire the check when necessary. ;)

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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  

  • Similar Content

    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      First, I want to apologize with you for the title not very clear, but I thought a little bit on which title to set at the thread, and this is the one that came in mind.
      Now, let me explain what I'm trying to understand.

      I am making an application ( with n clients ), with an SQLite Database, which has a Main form, and n sub-forms... 
      Since these sub-forms are called several times, because they are used to add or update records to the Database, I put the code that generates the GUI of these sub-forms in Functions.
      There are two sub-forms which are "treated" as two main forms, since even them have n sub-forms, which perform various tasks.
      Where am I stucked?

      For the Main form I created a Status Bar, in order to display messages ( add/delete/update/search results ), and it works perfectly with all the forms which don't have other sub-forms.
      For the two forms, which indeed have n sub-forms, I created two separated Status Bars, which are (re)created everytime I call the function that then displays the form.
      Practically, I can't set icons to these two Status Bars, and I don't know why.
      I'm really sorry that I don't feel clear enough, so, here you are an example of what I am saying.
      I recreated exactly what my script does:
      #include <GuiButton.au3> #include <GUIConstantsEx.au3> #include <GuiStatusBar.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) Global $frmSubForm, _ $txtSubFormField, _ $sbSubFormStatusBar, _ $objSB_IconError = _WinAPI_LoadShell32Icon(109), _ $objSB_IconInformation = _WinAPI_LoadShell32Icon(277) #Region ### START Koda GUI section ### Form= $frmMainForm = GUICreate("Main Form", 300, 300, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication") $sbMainStatusBar = _GUICtrlStatusBar_Create($frmMainForm) $btnCreateSubForm = GUICtrlCreateButton("Create Sub Form", 105, 110, 75, 41, BitOR($BS_CENTER,$BS_VCENTER,$BS_MULTILINE)) GUICtrlSetOnEvent(-1, "GenerateSubForm") GUICtrlSetFont(-1, 10, 400, 0, "Arial") _GUICtrlStatusBar_SetSimple($sbMainStatusBar) _GUICtrlStatusBar_SetText($sbMainStatusBar, "") GUISetState(@SW_SHOW, $frmMainForm) #EndRegion ### END Koda GUI section ### While 1 Sleep(100) WEnd Func ExitApplication() Exit EndFunc Func GenerateSubForm() #Region ### START Koda GUI section ### Form= $frmSubForm = GUICreate("Sub-Form", 300, 300, -1, -1, -1, -1, $frmMainForm) GUISetOnEvent($GUI_EVENT_CLOSE, "DeleteSubForm") $txtSubFormField = GUICtrlCreateInput("", 0, 0, 100, 20) $btnCheckSubFormField = GUICtrlCreateButton("Check Field", 105, 110, 75, 41, BitOR($BS_CENTER,$BS_VCENTER,$BS_MULTILINE)) GUICtrlSetOnEvent(-1, "CheckSubFormField") GUICtrlSetFont(-1, 10, 400, 0, "Arial") $sbSubFormStatusBar = _GUICtrlStatusBar_Create($frmSubForm) _GUICtrlStatusBar_SetSimple($sbSubFormStatusBar) _GUICtrlStatusBar_SetText($sbSubFormStatusBar, "") GUISetState(@SW_SHOW, $frmSubForm) GUISetState(@SW_DISABLE, $frmMainForm) #EndRegion ### END Koda GUI section ### EndFunc Func DeleteSubForm() ; Declared Globally => $frmSubForm GUIDelete($frmSubForm) GUISetState(@SW_ENABLE, $frmMainForm) EndFunc Func WriteToStatusBar($sbStatusBar, $objSB_Icon, $strText) _GUICtrlStatusBar_SetIcon($sbStatusBar, 0, $objSB_Icon) _GUICtrlStatusBar_SetText($sbStatusBar, $strText) ConsoleWrite("Icon Handle: " & $objSB_Icon & @CRLF) EndFunc Func CheckSubFormField() ; Declared Globally => $txtSubFormField If GUICtrlRead($txtSubFormField) = "" Then ; Declared Globally => $sbStatusBar WriteToStatusBar($sbSubFormStatusBar, $objSB_IconError, "Field NOK.") Else WriteToStatusBar($sbSubFormStatusBar, $objSB_IconInformation, "Field OK.") EndIf EndFunc The text is set, but icon is not.
      Could someone please tell me why?
      Thanks everyone in advance  

      Best Regards.
    • Reziskonh
      By Reziskonh
      Hi, everybody
      I look for GUI whose behavior similar to the message of an email client or antivirus
      In other words:
          Any PC screen able to calculate the size
          To consider the Task bar size
          To find the lower corner on the right/below and to nestle on it

      Notes:
      In GUI the GUICtrlCreateEdit field (as option) - that can be received and displayed through variable information
      Thanks a lot
      PS
      I use the translator, excuse if something is not clear
       

    • VollachR
      By VollachR
      Hi,
      I'd like to show a progress bar for an operation performed by an external program my script is running silently, I want to show it in a GUI I created using the GUICtrlCreateProgress but I have no idea how to do it.
      The important thing to point out is that there's no way of knowing how long the external program will run, as it is a file splitter and it depends on the size of file it splits and the split parts size.
      Can someone point me in the right direction or give me an example how to do so?
      This is my RunWait command:
      RunWait($MYFILES1 & '\fsplit.exe -split ' & $Size & ' mb ' & $File & " -f " & $File & "." & $extension) It uses multiple variables declared and set earlier in the script, how will I got about having the progress of that command shown using GUICtrlCreateProgress ? Is it possible?
      Thank you.
    • nacerbaaziz
      By nacerbaaziz
      Hello my friends
      I have an inquiry and I hope to find the answer here
      I want to create a graphical user interface
      but I want to hide the system menu
      I mean the window menu
      Is this possible?
      If is possible please give me how to do that
      Thanks in advance
    • helmar
      By helmar
      I was playing around with simple GUI creation.  I tend to like parameter driven coding (in prior life (years ago) as Clipper/FoxPro/dBase coder).  Just wanted to see what I could do with a GUI.
      #Region options, includes Opt('MustDeclareVars', 1) Opt("GUIOnEventMode", 1) ; Change to OnEvent mode #include <GUIConstantsEx.au3> #EndRegion options, includes Global Const $nCols = 2, $nRows = 6, $nSpacer = 10, $nBtnWidth = 150, $nBtnHeight = 30 Global $xName = 0, $xID = 0 Global $nGUIWidth = ($nCols * $nBtnWidth) + (($nCols + 1) * $nSpacer) Global $nGUIHeight = ($nRows * $nBtnHeight) + (($nRows + 1) * $nSpacer) Global $hMainGUI = GUICreate("Calculated GUI", $nGUIWidth, $nGUIHeight, -1, -1) For $xRows = 1 to $nRows ;in this arrangement, tabbing is left to right, then next row For $xCols = 1 to $nCols ;reverse the order of this line with the prior line for top to bottom tabbing, then next col $xName += 1 $xID += 1 Global $Dummy = GUICtrlCreateButton("Button" & $xName, _ ($nBtnWidth * ($xCols - 1)) + (($xCols - 1) * $nSpacer) + $nSpacer, _ ($nBtnHeight* ($xRows - 1)) + (($xRows - 1) * $nSpacer) + $nSpacer, _ $nBtnWidth, _ $nBtnHeight) GUICtrlSetOnEvent($xID+2, "Handler") Next Next GUISetOnEvent($GUI_EVENT_CLOSE, "CloseApp") GUISetState(@SW_SHOW) While 1 Sleep(10) WEnd Func CloseApp() Exit EndFunc Func Handler() MsgBox(0,0,"Button " & @GUI_CtrlId - 2) EndFunc  
×