Jump to content

While Loop Stopping GuiOnEventMode Actions


Go to solution Solved by Melba23,

Recommended Posts

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]

Link to comment
Share on other sites

  • Moderators
  • Solution

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

×
×
  • Create New...