美文网首页
core 异步阻塞

core 异步阻塞

作者: 杰克_王_ | 来源:发表于2021-04-27 11:38 被阅读0次
                Console.WriteLine(DateTime.Now);
                CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
                CancellationToken stoppingToken = cancellationTokenSource.Token;
    
                try
                {
                    Task.Factory.StartNew(async () =>
                    {
                        Console.WriteLine("before");
                        await Task.Delay(Timeout.Infinite, stoppingToken);
                        Console.WriteLine("after");
                    }, stoppingToken, TaskCreationOptions.LongRunning, TaskScheduler.Current)
                    .Unwrap().Wait();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
    
                Console.WriteLine(DateTime.Now);
    

    可根据实际情况将Wait改为 async await

    相关文章

      网友评论

          本文标题:core 异步阻塞

          本文链接:https://www.haomeiwen.com/subject/yhpurltx.html