监视具有超时和取消功能的异步任务

本文关键字:功能 异步 任务 取消 超时 监视 | 更新日期: 2023-09-27 18:00:28

I全部,

我必须监视异步任务,该任务必须可取消,并且执行的时间不能超过特定的生存时间

我已经知道下面的代码了。

CancellationTokenSource l_cts = new CancellationTokenSource(timemillis);

它将执行取消操作(到目前为止,我在异步方法中监视令牌)。然而,这并没有给我任何关于为什么他被取消、超时或用户取消的信息此外,超时事件被延迟,而我没有用捕捉到取消

Token.ThrowIfCancellationRequested();

为了解决这些问题,我编写了如下超时过程。

    static async Task TestAsync(int processDelaySeconds, int cancelDelaySeconds, int timeoutDelaySeconds )
    {
        CancellationTokenSource l_cts = new CancellationTokenSource();
        // the process to monitor
        Task l_process = new Task((state) =>
        {
            Console.WriteLine("Process BEGIN");
            // dummy loop
            for (int l_i = 0; l_i != processDelaySeconds; l_i++)
            {
                Thread.Sleep(1000);
                l_cts.Token.ThrowIfCancellationRequested();
            }
            Console.WriteLine("Process END");
        }, null, l_cts.Token);
        // register timeout
        RegisteredWaitHandle l_rwh = ThreadPool.RegisterWaitForSingleObject(l_cts.Token.WaitHandle,
                (state, timedOut) =>
                {
                    if (timedOut)
                    {
                        l_cts.Cancel();
                        Console.WriteLine("Timed out");
                    }
                    else
                    {
                        Console.WriteLine("Cancel Signaled");
                    }
                },
                null, (int)TimeSpan.FromSeconds(timeoutDelaySeconds).TotalMilliseconds, true);
        // cancel task
        if (cancelDelaySeconds > 0)
        {
            Task l_cancel = new Task(() =>
            {
                Thread.Sleep(TimeSpan.FromSeconds(cancelDelaySeconds));
                l_cts.Cancel();
            });
            l_cancel.Start();
        }
        try
        {
            l_process.Start();
            await l_process;
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine("Task Cancelled");
        }
        finally
        {
            // be sure to unregister the wait handle to cancel the timeout
            if (l_process.Status != TaskStatus.Canceled) l_rwh.Unregister(l_cts.Token.WaitHandle);
        }
        Console.WriteLine("Task Status is : {0}", l_process.Status);
    }  
    static async void Tests()
    {
        Console.WriteLine("NORMAL PROCESS");
        Console.WriteLine("--------------");
        await TestAsync(2, 10, 10);
        Console.WriteLine();
        Console.WriteLine("CANCEL");
        Console.WriteLine("------");
        await TestAsync(5, 2, 10);
        Console.WriteLine();
        Console.WriteLine("TIMEOUT");
        Console.WriteLine("-------");
        await TestAsync(10, 15, 2); 
    }

那么我的问题是:这背后有什么缺点或陷阱吗?一种更好、更有效的方式??

ps-目标是性能,而不是更短的代码。

监视具有超时和取消功能的异步任务

如果需要区分用户取消和超时取消,可以使用CreateLinkedTokenSource:

using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp
{
    internal class Program
    {
        // worker
        private static void DoWork(CancellationToken token)
        {
            for (int i = 0; i < 1000; i++)
            {
                token.ThrowIfCancellationRequested();
                Thread.Sleep(100); // do the work item
            }
            token.ThrowIfCancellationRequested();
        }
        // test
        private static void Main()
        {
            var userCt = new CancellationTokenSource();
            var combinedCt = CancellationTokenSource.CreateLinkedTokenSource(
                userCt.Token);
            combinedCt.CancelAfter(3000); // cancel in 3 seconds
            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                userCt.Cancel();
            };
            var task = Task.Run(
                () => DoWork(combinedCt.Token), 
                combinedCt.Token);
            try
            {
                task.Wait();
            }
            catch (AggregateException ex)
            {
                Console.WriteLine(ex.InnerException.Message);
                if (task.IsCanceled)
                {
                    if (userCt.Token.IsCancellationRequested)
                        Console.WriteLine("Cancelled by user");
                    else if (combinedCt.Token.IsCancellationRequested)
                        Console.WriteLine("Cancelled by time-out");
                    else 
                        Console.WriteLine("Cancelled by neither user nor time-out");
                }
            }
        }
    }
}

至于您的原始代码,您真的不需要ThreadPool.RegisterWaitForSingleObject(l_cts.Token.WaitHandle, ...),有CancellationToken.Register,它返回一个IDisposable,可以与using一起使用。

为了知道您的任务是否已取消或超时,您可以使用Task.WaitAny重载,该重载需要TimeSpan:

// Index will return -1 if timeout has occured, otherwise will print the index of the completed task
var cnclToken = new CancellationTokenSource().Token
var yourTask = Task.Run(() => { /* Do stuff */ }, cnclToken);
var index = Task.WhenAny(new[] { yourTask }, TimeSpan.FromSeconds(1));

http://msdn.microsoft.com/en-us/library/dd235645(v=vs.110).aspx