使用超时和取消监视异步任务

时间:2014-04-27 15:39:42

标签: c# .net async-await .net-4.5

我全部,

我必须监控异步任务,该任务必须可取消且执行速度不超过特定生存时间

我已经知道以下代码。

CancellationTokenSource l_cts = new CancellationTokenSource(timemillis);

将执行取消(至于我在我的异步方法中监视令牌)。 但是,这没有给我任何关于为什么他被取消,超时或用户取消?的信息,此外,超时事件被延迟,而我没有赶上取消< / p>

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-目标是性能,而不是更短的代码。

2 个答案:

答案 0 :(得分:3)

如果您需要区分用户和超时取消,可以使用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,可以使用{{1}}

答案 1 :(得分:1)

为了知道您的任务是否已被取消或超时,您可以使用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