如何取消等待超时的任务而不抛出异常 [英] How to cancel a task that is waiting with a timeout without exceptions being thrown

查看:37
本文介绍了如何取消等待超时的任务而不抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用取消令牌取消有超时的任务(在超时结束之前)时,会抛出异常.示例:

When canceling a task that has a timeout (before the timeout has ended) using a cancel token an exception is thrown. Example:

mytask.start();
bool didTaskRunInTime = mytask.wait(5 mins, _cancelToken);

这意味着我不能像下面那样继续.

Which means I cannot go on like below.

//was the task cancelled
if (_cancelToken.IsCancelRequested)
{
    // log cancel from user to file etc
}

if (didTaskRunInTime )
{
    int taskResult = myTask.Result;
    // log result to file
}
else if (!_cancelToken.IsCancelRequested)
{
    // Tell user task timed out , log a message etc
}

我将不得不在我的 catch 块中完成所有这些,我的代码看起来很乱.这样做的正确方法是什么?

I will have to do all this in my catch block and my code is looking messy. What is the correct way to do this?

推荐答案

您可以拨打 Task.WaitAny 带有只是那个任务的数组.然后您可以对任务的状态采取行动,但是该方法会返回.示例代码:

You could call Task.WaitAny with an array of just that task. Then you can act on the status of the task, however the method returns. Sample code:

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

class Test
{
    static void Main()
    {
        Task sleeper = Task.Factory.StartNew(() => Thread.Sleep(100000));

        int index = Task.WaitAny(new[] { sleeper },
                                 TimeSpan.FromSeconds(0.5));
        Console.WriteLine(index); // Prints -1, timeout

        var cts = new CancellationTokenSource();

        // Just a simple wait of getting a cancellable task
        Task cancellable = sleeper.ContinueWith(ignored => {}, cts.Token);

        // It doesn't matter that we cancel before the wait
        cts.Cancel();

        index = Task.WaitAny(new[] { cancellable },
                             TimeSpan.FromSeconds(0.5));
        Console.WriteLine(index); // 0 - task 0  has completed (ish :)
        Console.WriteLine(cancellable.Status); // Cancelled
    }
}

请注意,如果任务出错,您应该观察"异常,以避免在完成时发生异常:)

Note that if the task is faulted, you should "observe" the exception in order to avoid it going bang when it's finalized :)

这篇关于如何取消等待超时的任务而不抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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