I am currently using Florian Leitner's HID USB library in my VB.NET solution for communicating with a pin pad. As per his sample code, I set up an event handler to handle incoming responses from the device which are stored in an ArrayList called usbBuffer:
Private Sub BufferEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)
If USBInterface.usbBuffer.Count > 0 Then
While USBInterface.usbBuffer(0) Is Nothing
SyncLock USBInterface.usbBuffer.SyncRoot
USBInterface.usbBuffer.RemoveAt(0)
开发者_JAVA技巧 End SyncLock
End While
_receiveArray = CType(USBInterface.usbBuffer(0), Byte())
_usbInterface.stopRead()
SyncLock USBInterface.usbBuffer.SyncRoot
USBInterface.usbBuffer.RemoveAt(0)
End SyncLock
End If
End Sub
The problem is that the RemoveAt is not working, since the first element in the list remains there after the handler is done. Could someone please advise as to what I've done wrong, or perhaps use a different approach?
msdn says that the object of the synclock cannot be nothing. and you cannot cjhange the value of the lockobject.
msdn http://msdn.microsoft.com/en-us/library/3a86s51t(VS.80).aspx says
Rules
Lock Object Value. The value of lockobject cannot be Nothing. You must create the lock object before you use it in a SyncLock statement.
You cannot change the value of lockobject while executing a SyncLock block. The mechanism requires that the lock object remain unchanged.
精彩评论