0
我的应用程序有时会退出,没有任何例外。我在一个单独的线程中使用循环来读取HID设备。我认为问题在那里。但我不确定是什么原因导致了这个问题。我的代码有什么问题?VB.NET应用程序无一例外地退出
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
捕捉异常并重新抛出它是毫无意义的。如果异常未处理,它会自动冒出堆栈。你不必(也不应该)帮助它。完全从第一个函数中删除'Try' /'Catch'块。 –
我在没有Try/Catch的情况下做,但问题无法解决。谢谢。 –
如果应用程序因未处理的异常而崩溃,应用程序事件日志中应该包含异常的堆栈跟踪。 – Nicholas