CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2013
    Posts
    3

    vb6 multithread runs 21 times then stops and errors

    Hi,
    I have a project that has to read the usb port all the time and look at the data to parse it out and put some strings in a file.
    The following code will read the usb port and put the data into a circular buffer so that the top level application can read the data and then process it.
    This code seems to run 21 times before it just exits.
    I do not know what would cause such behavior,
    Can anyone offer any suggestions?
    Thanks,
    Martin

    Code:
    Public Sub WaitForUsbRet()
    Dim EventObject As Long
    Dim Result As Long
    Dim res2 As Integer
    Dim Security As SECURITY_ATTRIBUTES
    Dim RecBuffer(65) As Byte
    Dim rdNum As Long
    Dim i As Integer
    Dim res3 As Long
    res2 = 1
    
        Security.lpSecurityDescriptor = 0
        Security.bInheritHandle = CInt(True)
        Security.nLength = Len(Security)
            
        EventObject = CreateEvent(Security, CInt(True), CInt(False), "rdUsb")
        HIDOverlapped.Offset = 0
        HIDOverlapped.OffsetHigh = 0
        HIDOverlapped.hEvent = EventObject
    
        Do
            Form1.ShpStatus.BackColor = Form1.ShpStatus.BackColor + 1
            DoEvents
            RecBuffer(4) = 0
            RecBuffer(5) = 0
            Result = ReadFile _
            (HIDHandle, _
            RecBuffer(0), _
            CLng(Capabilities.InputReportByteLength), _
            rdNum, _
            HIDOverlapped)
    
            Form1.lstBoxRec.AddItem "Res=" & Result & " rdnum=" & rdNum & " hid=" & HIDOverlapped.Internal
    
            Result = WaitForSingleObject(EventObject, 500)
            
            Form1.lstBoxRec.AddItem "Res2=" & Result & " err=" & GetLastError & " hid=" & HIDOverlapped.Internal
    
            Select Case Result
                Case WAIT_TIMEOUT
                ' Timeout error.
                    Form1.lstBoxRec.AddItem "Out"
                    CancelIo (ReadHandle)
                    Form1.devStatus = RET_USBDEVREVTOOLOW
                    DoEvents
                Case WAIT_FAILED Or WAIT_ABANDONED
                    Form1.lstBoxRec.AddItem "FAIL"
                    DoEvents
                Case WAIT_OBJECT_0
        '        ' Success
        '        ' Use the report data.
                    rdNum = (RecBuffer(4) + 5)
                    If rdNum > 65 Then rdNum = 65
                   If (rdNum > 5) Then
                        For i = 1 To rdNum
                           Form1.putInCircBuff (RecBuffer(i))
                        Next i
                    End If
                    Form1.lstBoxRec.ListIndex = Form1.lstBoxRec.ListCount - 1
                    If (rdNum > 0) Then Form1.tmrCommHndl.Enabled = True
                    DoEvents
                Case Else
                    Form1.lstBoxRec.AddItem "Else"
                    CancelIo (ReadHandle)
                    Form1.devStatus = RET_USBDEVREVTOOLOW
            End Select
            res3 = ResetEvent(EventObject)
        Loop Until res2 = 0
    End Sub
    Last edited by GremlinSA; September 19th, 2013 at 01:46 AM. Reason: added code tags..

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: vb6 multithread runs 21 times then stops and errors

    Can't tell from what you posted, the loop runs until res2=0 and res2 is not being changed in the loop so it should run forever i.e. infinite loop

    What does CancelTo() do? I would suspect there is something there that is stopping it

    What is this line doing
    Code:
    EventObject = CreateEvent(Security, CInt(True), CInt(False), "rdUsb")
    ?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2013
    Posts
    3

    Re: vb6 multithread runs 21 times then stops and errors

    Hi,
    The issue was that the thread and the top level were all writing to the list box so eventually there would be some kind of interrupt/run-time collision

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured