zoukankan      html  css  js  c++  java
  • C#Timer

     protected virtual async Task ScheduleJob(CancellationToken cancellationToken)
            {
                var next = _expression.GetNextOccurrence(DateTimeOffset.Now, _timeZoneInfo);
                if (next.HasValue)
                {
                    var delay = next.Value - DateTimeOffset.Now;
                    _timer = new System.Timers.Timer(delay.TotalMilliseconds);
                    _timer.Elapsed += async (sender, args) =>
                    {
                        _timer.Dispose();  // reset and dispose timer
                        _timer = null;
    
                        if (!cancellationToken.IsCancellationRequested)
                        {
                            await DoWork(cancellationToken);
                        }
    
                        if (!cancellationToken.IsCancellationRequested)
                        {
                            await ScheduleJob(cancellationToken);    // reschedule next
                        }
                    };
    
                    System.Threading.Timer timer = new Timer(new TimerCallback(async (obj) =>
                  {
                      _timer.Dispose();  // reset and dispose timer
                        _timer = null;
    
                      if (!cancellationToken.IsCancellationRequested)
                      {
                          await DoWork(cancellationToken);
                      }
    
                      if (!cancellationToken.IsCancellationRequested)
                      {
                          await ScheduleJob(cancellationToken);    // reschedule next
                        }
                  }), null, 0, System.Threading.Timeout.Infinite);
                    _timer.Start();
                }
                await Task.CompletedTask;
            }
    

      

  • 相关阅读:
    轮播闪白效果
    轮播图效果
    打字游戏简约版
    js购物时的放大镜效果
    单例模式
    docker
    【spring】注解梳理
    【spring源码-1】BeanFactory + XMLBeanFactory
    【设计模式】
    【大数据Spark】PC单机Spark开发环境搭建
  • 原文地址:https://www.cnblogs.com/francisXu/p/13092810.html
Copyright © 2011-2022 走看看