Skip to main content
 首页 » 编程设计

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

2024年02月13日12yjmyzz

我都是,

我必须监控一个异步任务,该任务必须可取消并且执行的时间不会超过特定的生存时间。

我已经知道以下代码。

CancellationTokenSource l_cts = new CancellationTokenSource(timemillis); 

这将执行取消(到目前为止我在我的异步方法中监视 token )。 但是,这没有给我任何关于他被取消的原因的信息,超时或用户取消?此外,超时事件被延迟,而我没有用

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 一起使用。