2013-04-09 32 views
10

我的切入點應該趕在較低的水平沒有被處理的任何異常:頂級例外不會一無所獲

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 
using System.Threading; 
using System.Runtime.InteropServices; 

namespace EyeScanner 
{ 
    static class Program 
    { 
     [FlagsAttribute] 
     public enum EXECUTION_STATE : uint 
     { 
      ES_AWAYMODE_REQUIRED = 0x00000040, 
      ES_CONTINUOUS = 0x80000000, 
      ES_DISPLAY_REQUIRED = 0x00000002, 
      ES_SYSTEM_REQUIRED = 0x00000001 
      // Legacy flag, should not be used. 
      // ES_USER_PRESENT = 0x00000004 
     } 

     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_AWAYMODE_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED); 

      bool isNew; 
      Mutex m = new Mutex(false, "EyeScannerByOphthaMetrics", out isNew); 
      try 
      { 
       if (isNew) Application.Run(new CheckSystemForm()); 
       else { MessageBox.Show("An other instance of EyeScanner is running"); } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.StackTrace);     
      } 
     } 
    } 
} 

現在,我會想到去年catch()趕上這是沒有處理任何異常,但我有一種情況(100%可重現)來創建一個NullReferenceException(關閉系統的硬件),但我不能斷點它,因爲它發生在一個隨機點,我無法捕捉它,並且我無法獲得調用堆棧(當我嘗試調試它,並且Visual Studio說發生異常時,沒有CallStack,也沒有可用的反彙編)。我在調試菜單中激活了NullReferenceException,但沒有發現。

我該如何面對呢?我得到了一個100%的異常,但是我無法調試它來找出它會發生的原因。

編輯: 事件日誌:

Anwendung: EyeScanner.exe 
Frameworkversion: v4.0.30319 
Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet. 
Ausnahmeinformationen: System.ObjectDisposedException 
Stapel: 
    bei System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean ByRef) 
    bei System.StubHelpers.StubHelpers.SafeHandleAddRef(System.Runtime.InteropServices.SafeHandle, Boolean ByRef) 
    bei Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(Microsoft.Win32.SafeHandles.SafeFileHandle, System.Threading.NativeOverlapped*, Int32 ByRef, Boolean) 
    bei System.IO.Ports.SerialStream+EventLoopRunner.WaitForCommEvent() 
    bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
    bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 
    bei System.Threading.ThreadHelper.ThreadStart() 

我完全不知道到哪裏尋找這個錯誤...

EDIT2:

我用SafeSerialStream現在,錯誤發生在那裏:

protected override void Dispose(bool disposing) 
     { 
      if (disposing && (base.Container != null)) 
      { 
       base.Container.Dispose(); 
      } 
      try 
      { 
       if (theBaseStream.CanRead) 
       { 
        theBaseStream.Close(); 
        GC.ReRegisterForFinalize(theBaseStream); 
       } 
      } 
      catch 
      { 
       // ignore exception - bug with USB - serial adapters. 
      } 
      base.Dispose(disposing); 
     } 

該代碼崩潰正好試圖執行theBaseStream.Close();,儘管它在try{] catch{}塊中。我能做些什麼?

編輯3:崩潰

控制檯輸出:

A first chance exception of type 'System.IO.IOException' occurred in System.dll 
A first chance exception of type 'System.IO.IOException' occurred in System.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.dll 
'EyeScanner.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Transactions.resources\v4.0_4.0.0.0_de_b77a5c561934e089\System.Transactions.resources.dll' 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unbehandelte Ausnahme</Description><AppDomain>EyeScanner.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message><StackTrace> bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</StackTrace><ExceptionString>System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. 
    bei System.StubHelpers.StubHelpers.CheckCollectedDelegateMDA(IntPtr pEntryThunk)</ExceptionString></Exception></TraceRecord> 
+7

不同的線程,也許?看起來像最明顯的原因。 – 2013-04-09 14:37:08

+1

你不會碰巧通過USB串口適配器運行任何東西嗎? – 2013-04-09 14:39:33

+0

@JonSkeet:我們甚至可以捕獲非託管代碼拋出的異常嗎?這對我來說是全新的! – fjdumont 2013-04-09 14:39:45

回答

2

最後我用用USB /串口問題時(這是一個常數頭痛)的解決方案是與串行端口涉及到一個完全獨立的過程中,使用公開服務的代碼我分出一部分WCF。 WCF服務可以暴露直接映射(或者幾乎直接 - 這是一個很好的機會爲更低級別的東西添加一些抽象)到串行端口接口的函數,並且如果進程熄滅,你可以從你的main沒有主應用程序的應用程序。

1

這看起來像一個線程被殺死你的過程中未處理的異常。此行爲在MSDN中描述here

作爲測試,通過應用配置文件調用傳統異常處理策略,如文章中所述。在<runtime>部分,添加以下設置:

<legacyUnhandledExceptionPolicy enabled="1"/> 

然後看看你的catch拾起異常,或者至少如果異常堆棧跟蹤寫入控制檯。

希望有所幫助。

+0

不,沒有幫助。這似乎是USB串行微控制器的一個愚蠢的問題。我想把它放到另一個線程左右,但問題是,即使我知道似乎正在發生的事情,我無法避免它。 – SinisterMJ 2013-04-10 14:00:20

+0

並且在調試輸出控制檯上沒有任何內容? :-( – 2013-04-10 14:06:01

+0

是的,我把它放在編輯3上,不知道它應該告訴我什麼。它沒有調用堆棧,雖然 – SinisterMJ 2013-04-10 14:29:02

0

也許,你是不是捕捉異常,因爲你沒有得到它裏面的try-catch? 以防萬一,是的......你能放線 Mutex m = new Mutex(false, "EyeScannerByOphthaMetrics", out isNew); 您try塊內?

+0

不,它確實是由我的USB串行控制器引起的,我知道它工作。 – SinisterMJ 2013-04-12 11:51:43

相關問題