如何使应用程序崩溃时,一个任务抛出一个异常,而无需等待终结 [英] How to make an application crash when a Task throws an exception without waiting the finalizer

查看:469
本文介绍了如何使应用程序崩溃时,一个任务抛出一个异常,而无需等待终结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将在我们的.NET 4中使用的任务(无异步等待可用)的应用程序,有时它们被用来发射发射后不管类似以下操作:

 私人无效测试()
{
    任务task = Task.Factory.StartNew(()=>
    {
        抛出新ApplicationException的(测试);
    });
}
 

我们想让该异常崩溃的应用程序,而无需等待任务(否则是没有意义有它的一个任务,至少在我们的情况),没有等待的终结,因为我们要关闭应用程序时,一个意想不到的发生错误,避免状态损坏(我们保存状态present当异常发生)。

我的猜测是,不知怎的,我们应该有后续任务工作,但会将延续code里面还有一项任务,不会使应用程序崩溃,所以我在这里封锁。

任何帮助将是非常美联社preciated

编辑:如果切换到线程池的结果是所预期的。下面code的应用程序崩溃:

  ThreadPool.QueueUserWorkItem((C)=>
{
    抛出新ApplicationException的(测试);
});
 

解决方案

我终于找到了如何做到这一点,即使它是一个有点复杂:

 命名空间ThreadExceptions
{
    使用系统;
    使用的System.Threading;
    使用System.Threading.Tasks;

    公共静态类TaskExtensions
    {
        公共静态任务ObserveExceptions(这个任务的任务)
        {
            返回task.ContinueWith((吨)=>
            {
                ThreadPool.QueueUserWorkItem((瓦特)=&G​​T;
                {
                    如果(t.Exception!= NULL)
                    {
                        的foreach(在t.Exception.InnerExceptions异常前)
                        {
                            抛出新TaskException(前);
                        }
                    }
                });
            },TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions preferFairness)。
        }
    }
}
 

这会使应用程序崩溃,而无需等待任务。这是我所期待的。

We are using Tasks in our .Net 4 (no async await available) application and sometimes they are used to launch 'Fire and Forget' operations like the following one:

private void Test()
{
    Task task = Task.Factory.StartNew(() =>
    {
        throw new ApplicationException("Test");
    });
}

We want this exception to crash the application without waiting the task (as otherwise it makes no sense to have it in a task, at least in our scenarios) and without waiting the finalizer as we want to shutdown the application when an unexpected error happens to avoid state corruptions (we are saving the state present when the exception happened).

My guess is that somehow we should work with a continuation task but that puts the continuation code inside another task that will not make the application crash so I'm blocked here.

Any help will be very appreciated

Edit: if switching to the ThreadPool the result is the expected one. The following code crashes the application:

ThreadPool.QueueUserWorkItem((c) =>
{
    throw new ApplicationException("Test");
});

解决方案

I finally found how to do it even when it is a bit complicated:

namespace ThreadExceptions
{
    using System;
    using System.Threading;
    using System.Threading.Tasks;

    public static class TaskExtensions
    {
        public static Task ObserveExceptions(this Task task)
        {
            return task.ContinueWith((t) =>
            {
                ThreadPool.QueueUserWorkItem((w) =>
                {
                    if (t.Exception != null)
                    {
                        foreach (Exception ex in t.Exception.InnerExceptions)
                        {
                            throw new TaskException(ex);
                        }
                    }
                });
            }, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.PreferFairness);
        }
    }
}

This will make the application crash without waiting for the task. That's was I was looking for.

这篇关于如何使应用程序崩溃时,一个任务抛出一个异常,而无需等待终结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆