我的入口点应该捕获任何未在较低级别处理的异常:
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()
我完全不知道去哪里寻找这个错误...
编辑2:
我现在用的是那个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{}
block 中。我该怎么办?
编辑 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>
最佳答案
最终,我用于处理 USB/串行端口问题(这一直是一个令人头疼的问题)的解决方案是将处理串行端口的代码部分分离到一个完全独立的进程中,该进程使用 WCF 公开服务. WCF 服务可以公开直接映射(或几乎直接 - 这是为较低级别的东西添加一些抽象的好机会)到串行端口接口(interface)的函数的方法,如果进程崩溃,你可以从你的 main 重新启动它没有主应用程序运行的应用程序。
关于c# - 顶级异常没有捕获任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15904854/