2012-01-25 7 views
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 
+2

例外をキャッチして再スローするのは全く意味がありません。例外が処理されていない場合は、スタックを自動的にバブルアップします。あなたはそれを手助けする必要はありません。あなたの最初の関数から 'Try' /' Catch'ブロックを完全に削除してください。 –

+0

私はTry/Catchなしでやっていますが、問題は解決できません。ありがとう。 –

+0

処理されていない例外のためにアプリケーションがクラッシュした場合、アプリケーションのイベントログに例外のスタックトレースを含むエントリが存在するはずです。 – Nicholas

答えて

4

それは例外あるが提起されている可能性があります。 UIスレッドで例外が発生しないため、通常のクラッシュダイアログは表示されません。 catchブロック(コンソール出力、トレースログ、またはWindowsイベントビューア)の内部で、何らかの種類のデバッグ出力に例外を記録するようにしてください。

+0

私はtry catchを使ってログファイルを生成しようとしています。しかし、ログ結果はありません(例外も表示されません)。そしてアプリケーションはまた、理由なく終了します。 –

関連する問題