2013-04-09 10 views
10

mio punto di ingresso dovrebbe prendere alcuna eccezione non essere gestita ad un livello inferiore:eccezione di livello superiore non prendere nulla

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);     
      } 
     } 
    } 
} 

Ora ci si aspetta che lo scorso catch() per catturare qualsiasi eccezione che non viene affrontata, ma io avere una situazione (riproducibile al 100%) per creare un NullReferenceException (disattivare l'hardware collegato al sistema spento), ma non riesco a fermarlo poiché si verifica in un punto casuale, non riesco a prenderlo e non riesco a ottenere un Call Stack (quando provo a eseguirne il debug, e Visual Studio dice che si è verificata un'eccezione, non c'è CallStack e non è disponibile il disassemblaggio). Ho attivato il NullReferenceException nel menu di debug, ma non l'ho rilevato.

Come posso gestire questo? Ricevo un'eccezione con il 100%, ma non riesco a eseguirne il debug per scoprire perché potrebbe accadere.

Edit: L'EventLog:

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() 

non ho assolutamente idea di dove cercare questo errore ...

Edit2:

ho usato che SafeSerialStream ora, e l'errore si verifica in là:

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); 
     } 

Il codice si blocca esattamente un fter prova ad eseguire theBaseStream.Close();, anche se è in un blocco try{] catch{}. Cosa posso fare a riguardo?

Edit 3:

uscita Console su incidente:

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

Filo diverso, forse?Sembra la causa più ovvia. –

+1

Non avresti mai potuto eseguire qualcosa tramite un adattatore seriale USB? –

+0

@JonSkeet: possiamo anche rilevare eccezioni generate da codice non gestito? Era completamente nuovo per me! – fjdumont

risposta

2

In definitiva la soluzione che ho usato per gestire i problemi di porta USB/seriale (che sono un costante mal di testa) è separare la parte del mio codice che si occupa della porta seriale in un processo completamente separato che espone un servizio usando WCF. Il servizio WCF può esporre i metodi che mappano direttamente (o quasi direttamente - è una buona possibilità di aggiungere qualche astrazione per elementi di livello inferiore) alle funzioni dell'interfaccia della porta seriale e se il processo si esaurisce, puoi semplicemente riavviarlo dal tuo principale applicazione senza l'applicazione principale in corso.

1

Questo appare come un'eccezione non gestita in un thread sta uccidendo il processo. Questo comportamento è descritto in MSDN here

Come test, richiamare la politica di gestione delle eccezioni legacy tramite il file di configurazione dell'app come descritto nell'articolo. Nella sezione <runtime>, aggiungere la seguente impostazione:

<legacyUnhandledExceptionPolicy enabled="1"/> 

poi vedere se i tuoi catch preleva l'eccezione, o almeno se la traccia dello stack eccezione viene scritto nella console.

Spero che questo aiuti.

+0

No, non è stato d'aiuto. Sembra essere un problema stupido con il micro controller seriale USB. Sto pensando di metterlo in un altro thread o così, ma il problema è che anche se so cosa sembra accadere non posso evitarlo. – SinisterMJ

+0

Nulla sulla console di output di debug? :-( –

+0

Sì, l'ho messo su Modifica 3, non ho idea di cosa avrebbe dovuto dirmi lì. Il suo stack di chiamate non era – SinisterMJ

0

Forse non stai prendendo un'eccezione, perché non riesci a farcela con try-catch? Nel caso, per essere sicuri ... Puoi inserire la riga Mutex m = new Mutex(false, "EyeScannerByOphthaMetrics", out isNew); all'interno del tuo blocco try?

+0

No, è stato effettivamente causato dal mio USB-Serial Controller. – SinisterMJ

Problemi correlati