Valhalla Legends Forums Archive | Battle.net Bot Development | Problem with Message Queue

AuthorMessageTime
Dyndrilliac
Ok. I was making a new message Queue to go with my bot's facelift. Here are the relevant parts:

[code]Public Sub AddQ(Message As String)
Do Until Queue(QNum) = vbNullString
QNum = QNum + 1
Loop
Queue(QNum) = Message
End Sub[/code][code]'// Message Queue -- Declarations
Public Queue() As String
Public QNum As Integer
Public QDelay As Single[/code][code]Public Sub LoadConfig()
QNum = 0
ReDim Queue(0)
Queue(0) = vbNullString
...[/code][code]Private Sub tQueue_Timer()

If Connected = False Then
Exit Sub
End If

If Queue(QNum) = vbNullString Then
Exit Sub
End If

Dim i As Integer
For i = 0 To QNum
QDelay = ((Len(Queue(i)) / 10) / 2)
Pause QDelay, True
Send Queue(i)
Queue(i) = vbNullString
Next i
End Sub[/code][code]Sub Pause(ByVal fSeconds As Single, Optional ByVal AllowEvents As Boolean = True)
Dim fTimer As Single
If AllowEvents Then
fTimer = Timer
Do While Timer - fTimer < fSeconds
Sleep 100
DoEvents
If Timer < fTimer Then
fTimer = fTimer - 86400
End If
Loop
Else
Sleep fSeconds * 1000
End If
End Sub[/code]

Now. The problem is when I enter too many message into the Queue too quickly, I get a Subscript out of Range error in the AddQ Sub, posted above. Apart from that it works perfectly.

I was thinking a temporary fix would be to On Error, re initialize the array, clear it, and return to the Queue's flow...but I want to know if there is a way to prevent the error from happening at all. Other than that the system works pretty much perfectly.
June 2, 2004, 6:41 AM
Dyndrilliac
Hm, the problem may already be fixed...I modified the AddQ Function to basically fix the problem on it's own when it errored.

[code]Public Sub AddQ(Message As String)
On Error GoTo ErrorHandle

TryAgain:

Do Until Queue(QNum) = vbNullString
QNum = QNum + 1
Loop
Queue(QNum) = Message
Exit Sub

ErrorHandle:

QNum = 0
ReDim Queue(0)
Queue(0) = vbNullString
GoTo TryAgain

End Sub[/code]

P.S.: If any one would like to use this, by all means do so, and post any bugs you fix.

-- Edit --

Another bug I noticed is if someone enters a lot(like 4) small messages very quickly the Queue basically has no effect and the user flood off.

[code]Public iFloodThresh As Integer
Public sFloodThresh As String

Public Sub AddQ(Message As String)
On Error GoTo ErrorHandle
If sFloodThresh = Message Or Len(sFloodThresh) = Len(Message) Then
iFloodThresh = iFloodThresh + 1
If iFloodThresh >= 4 Then
Pause 2, True
End If
Else
iFloodThresh = 0
End If
TryAgain:
Do Until Queue(QNum) = vbNullString
QNum = QNum + 1
Loop
Queue(QNum) = Message
sFloodThresh = Message
Exit Sub
ErrorHandle:
QNum = 0
ReDim Queue(0)
Queue(0) = vbNullString
GoTo TryAgain
End Sub[/code]
June 2, 2004, 6:57 AM
CrAz3D
Wouldn't using ubound & such be easier than
[code]Do Until Queue(QNum) = vbNullString
QNum = QNum + 1
Loop
Queue(QNum) = Message
Exit Sub[/code]

like Queue(Ubound(Queue))=message
redim preserve queue(ubound(queue)+1)
June 2, 2004, 2:35 PM
The-FooL
Your error is coming from the fact that your not checking to see if the que is full. If it is full, then QNum will exceed the upper limit of the array, and the error occurs.
June 2, 2004, 7:52 PM
Dyndrilliac
Yea, I realized that and for the handler I just told it to clear the queue and try to add the message again.

Craz3d: Probably, I wasn't really thinking about that though.

Edit: [code]Queue(UBound(Queue)) = Message
ReDim Preserve Queue(UBound(Queue) + 1)[/code]Caused an infinite loop to take place making Visual Basic 6 to eat up 100% System Resources, Lol.
June 2, 2004, 8:25 PM
CrAz3D
How did that cause a loop?
June 2, 2004, 10:20 PM
Dyndrilliac
I don't know, but when I added a Message to the queue replacing my old code with what you posted, it went into an infinite loop and froze the bot.
June 2, 2004, 10:55 PM
CrAz3D
[quote author=Dyndrilliac link=board=17;threadid=7071;start=0#msg63341 date=1086216934]
I don't know, but when I added a Message to the queue replacing my old code with what you posted, it went into an infinite loop and froze the bot.
[/quote]
That is really weird.
June 3, 2004, 4:30 PM
Lenny
I recommend you to not use Pause which uses the Sleep function. This pauses the entire thread of your program and often causes jerky and unnatural response. (Using Sleep alone pauses your entire thread)
Look into the SetTimer API.

IIRC, Pause can't be used within another loop.
June 3, 2004, 7:03 PM

Search