我的应用程序有时退出,没有任何异常。我在一个单独的线程中使用循环来读取HID设备。我认为问题在那里。但我不确定是什么导致了这个问题。我的代码出了什么问题?
Private Sub USBListenServer()
If (readHandle IsNot Nothing) AndAlso (Not readHandle.IsInvalid) Then
Try
Dim dataBytes(&H40) As Byte
Dim bytesRead As Int32
Do
If (readHandle IsNot Nothing) AndAlso _
(Not readHandle.IsInvalid) AndAlso _
(Not readHandle.IsClosed) AndAlso _
HID_Read(hidHandle, readHandle, writeHandle, dataBytes, bytesRead) AndAlso _
(bytesRead > 0) Then
Dim myDataBytes(&H40) As Byte
Array.Resize(Of Byte)(myDataBytes, bytesRead - 1)
Array.Copy(dataBytes, 1, myDataBytes, 0, bytesRead - 1)
Dim _dataArrivedEvent As DataArrivedEventHandler = DataArrivedEvent
If _dataArrivedEvent IsNot Nothing Then
_dataArrivedEvent.Invoke(myDataBytes)
End If
End If
Loop While (listenServerRunning)
Catch ex As Exception
Throw New Exception(ex.Message, ex)
End Try
End If
Application.ExitThread()
End Sub
Private Function HID_Read(ByVal hHandle As SafeFileHandle, ByVal rHandle As SafeFileHandle, _
ByVal wHandle As SafeFileHandle, ByRef dataBytes As Byte(), _
ByRef BytesRead As Int32) As Boolean
Dim hidOverlapped As New NativeOverlapped
Dim eventObject As IntPtr
Dim nonManagedBuffer As IntPtr
Dim nonManagedOverlapped As IntPtr
Dim numberOfBytesRead As Int32
Dim result As Int32
Dim success As Boolean
' Setup the overlapped structure for the ReadFile.
PrepareForOverlappedTransfer(hidOverlapped, eventObject)
' Allocate memory for the input buffer and overlapped structure.
nonManagedBuffer = Marshal.AllocHGlobal(dataBytes.Length)
nonManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(hidOverlapped))
Marshal.StructureToPtr(hidOverlapped, nonManagedOverlapped, False)
' ***
' Attemps to read an Input Report from the device.
' Parameters:
' A device handle returned by the createfile. (for Overlapped I/O, CreateFile must has been called with FILE_FLAG_OVERLAPPED.)
' A pointer to a buffer for storing the report.
' The input report length in bytes returned by HidP_GetCaps.
' A pointer to a variable that will hold the number of bytes read.
' An overlapped structure whose hEvent member is set to an event object.
' The overlapped called returns immediately, even if the data hasn't been received yet.
' To read the multiple reports with one ReadFile, increase the size of ReadBuffer and use NumberOfBytesRead to determine how many reports were returned.
' Use a larger buffer if the application can't keep up with reading report individually.
' ***
success = ReadFile(rHandle, nonManagedBuffer, dataBytes.Length, numberOfBytesRead, nonManagedOverlapped)
If Not (success) Then
Select Case Marshal.GetLastWin32Error
Case &H3E5
''Debug.WriteLine("Waiting for ReadFile...") '//Comment out to avoid overload writing text file
' Wait for at least one report or a time-out. Used with overlapped ReadFile.
result = WaitForSingleObject(eventObject, 100)
' Find out if the ReadFile completed or timeout.
Select Case result
Case WAIT_OBJECT_0
' ReadFile has completed.
success = True
''Debug.WriteLine("ReadFile completed successfully.") //Comment out to avoid overload writing text file
' Get the number of bytes read.
'
' Get the result of an overlapped operation.
' Accepts:
' A device handle returned by CreateFile.
' A pointer to an overlapped structure.
' A pointer to a variable to hold the number of bytes read.
' False to return immediately.
GetOverlappedResult(rHandle, nonManagedOverlapped, numberOfBytesRead, False)
Case WAIT_TIMEOUT
' cancel the operation on timeout
CancelTransfer(hHandle, rHandle, wHandle, eventObject)
''Debug.WriteLine("ReadFile timeout.") //Comment out to avoid overload writing text file
success = False
Case Else
' cancels the operation on other error
CancelTransfer(hHandle, rHandle, wHandle, eventObject)
''Debug.WriteLine("ReadFile undefined errro.") //Comment out to avoid overload writing text file
success = False
End Select
End Select
End If
If success Then
' A report was received. Copy the received data to inputReportBuffer for the application to use.
Marshal.Copy(nonManagedBuffer, dataBytes, 0, numberOfBytesRead)
BytesRead = numberOfBytesRead
End If
If Not (nonManagedOverlapped = IntPtr.Zero) Then
Marshal.FreeHGlobal(nonManagedOverlapped)
End If
If Not (nonManagedBuffer = IntPtr.Zero) Then
Marshal.FreeHGlobal(nonManagedBuffer)
End If
Return success
End Function
答案 0 :(得分:4)
可能会引发异常 。由于 UI 线程未引发异常,因此您将看不到正常的崩溃对话框。您应该尝试将异常记录到catch块(控制台输出,跟踪日志或Windows事件查看器)中的某种调试输出中。