加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 顶级异常没有捕捉到任何东西

发布时间:2020-12-15 06:14:43 所属栏目:百科 来源:网络整理
导读:我的入口点应该捕获任何未被处理在较低级别的异常: using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using System.Threading;using System.Runtime.InteropServices;namespace EyeScanner{ static class Prog
我的入口点应该捕获任何未被处理在较低级别的异常:
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%的异常,但我无法调试它,以找出为什么会发生.

编辑:
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.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);
        }

在尝试执行BaseStream.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:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Transactions.resourcesv4.0_4.0.0.0_de_b77a5c561934e089System.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,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服务可以公开直接映射的方法(或几乎直接 – 这是一个很好的机会,为较低级别的东西添加一些抽象)到串行端口接口的功能,如果进程退出,您可以从主要重新启动它应用程序没有主要应用程序进行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读