UnobservedTaskException

UnobservedTaskException

本文介绍了在 dotnetcoreapp2.0 中使用 UnobservedTaskException 处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在 netcoreapp2.0 应用程序中运行时,似乎最终不会抛出 UnobservedTaskException

The following code, when run in a netcoreapp2.0 application, doesn't seem to end up throwing the UnobservedTaskException

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp3 {

    class Program {

        public static void Main(string[] args) {

            TaskScheduler.UnobservedTaskException += (s, e) => {
                /// Try to crash the application - we wanna nail unobserved exceptions dead.
                /// Unfortunately, this code never seems to run.
                Console.WriteLine("UnobservedTaskException thrown.");
                throw e.Exception; 
            };

            var t = Task.Run(() => {
                throw new NotImplementedException();
            });

            while (!t.IsFaulted)
                Thread.Sleep(1);

            t = null;
            Console.WriteLine("Task is faulted.");

            GC.Collect();
            GC.WaitForPendingFinalizers();
            Console.ReadKey();
        }

    }
}

这是项目文件.如何触发 UnobservedTaskException 处理程序?

Here's the project file. How can I get the UnobservedTaskException handler to be fired?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>

</Project>

在其他 stackoverflow 帖子中,我看到了在项目文件中使用以下代码段的建议,但它仅适用于 .net framework 4.0+ 项目.如果 .netcore 项目文件有等价物,那可能就是我需要知道的.

In other stackoverflow posts, I've seen advice to use the following snippet in the project file, but it only works for .net framework 4.0+ projects. If there's an equivalent for the .netcore project file, that might be what I need to know.

<runtime> 
    <ThrowUnobservedTaskExceptions enabled="true"/> 
</runtime> 

推荐答案

我遇到了同样的问题.只需尝试在 RELEASE 模式下运行即可.我对其进行了测试,它可以与控制台应用程序 .net 核心 2.2 版一起使用.

I had the same issue. Just try it run in RELEASE mode. I tested it and it's working with console application .net core version 2.2.

internal class Program
{
    private static void Main(string[] args)
    {
        // REMEMBER TO RUN IN RELEASE MODE

        var handler = new EventHandler<UnobservedTaskExceptionEventArgs>(Unobserved);
        TaskScheduler.UnobservedTaskException += handler;

        Task.Run(() => { Console.WriteLine("task 1"); throw new Exception("TASK 1 EXCEPTION"); });
        Task.Run(() => { Console.WriteLine("task 2"); throw new Exception("TASK 2 EXCEPTION"); });

        Thread.Sleep(1000);

        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();

        Thread.Sleep(1000);

        Console.ReadKey();
    }

    private static void Unobserved(object o, UnobservedTaskExceptionEventArgs e)
    {
        e.SetObserved(); // optional
        foreach (var ex in e.Exception.InnerExceptions)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

这篇关于在 dotnetcoreapp2.0 中使用 UnobservedTaskException 处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 14:38