• Welcome to Valhalla Legends Archive.
 

Problem with Message Queue

Started by Dyndrilliac, June 02, 2004, 01:41 AM

Previous topic - Next topic

Dyndrilliac

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

Public Sub AddQ(Message As String)
   Do Until Queue(QNum) = vbNullString
       QNum = QNum + 1
   Loop
   Queue(QNum) = Message
End Sub
'// Message Queue -- Declarations
Public Queue() As String
Public QNum    As Integer
Public QDelay  As Single
Public Sub LoadConfig()
   QNum = 0
   ReDim Queue(0)
   Queue(0) = vbNullString
   ...
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
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


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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Dyndrilliac

#1
Hm, the problem may already be fixed...I modified the AddQ Function to basically fix the problem on it's own when it errored.

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


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.

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
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

CrAz3D

Wouldn't using ubound & such be easier than
Do Until Queue(QNum) = vbNullString
       QNum = QNum + 1
   Loop
   Queue(QNum) = Message
   Exit Sub


like Queue(Ubound(Queue))=message
redim preserve queue(ubound(queue)+1)
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

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.

Dyndrilliac

#4
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: Queue(UBound(Queue)) = Message
ReDim Preserve Queue(UBound(Queue) + 1)
Caused an infinite loop to take place making Visual Basic 6 to eat up 100% System Resources, Lol.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

CrAz3D

rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

CrAz3D

Quote from: Dyndrilliac on June 02, 2004, 05:55 PM
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.
That is really weird.
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

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.
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.