zoukankan      html  css  js  c++  java
  • c# Timer按月执行任务

    直接贴代码:

            /// <summary>
            /// 每月导出
            /// </summary>
            private void ExportMonthExcel()
            {
                int t = 0;//以此变量控制第一次不执行
                _monthTimer = new System.Threading.Timer(execute, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
                _monthTimer.Change(TimeSpan.Zero, Timeout.InfiniteTimeSpan);
    
                void execute(object dateTime)
                {
                    //下个月1号早上7:30
                    var executeTime = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddHours(7.5);
                    //调试模式下,第一次访问设置下次执行时间
                    if (ConfigHelper.Debug)
                    {
                        if (t == 0)
                        {
                            executeTime = DateTime.Now.AddSeconds(3);
                        }
                    }
    
                    if (t > 0)
                    {
                        try
                        {
                            var columns = this._anomateexcelService.GetAll();
                            _monthdYTrackANODetail700013TodayService.ExportMonthToExcel(columns, executeTime);
                        }
                        catch (Exception ex)
                        {
                            _logger.Error($"每月导出excel任务失败,message:{ex.Message},stackTrace:{ex.StackTrace}");
                        }
                    }
    
                    //执行完后,重新设置定时器下次执行时间.
                    _monthTimer.Change(executeTime.Subtract(DateTime.Now), Timeout.InfiniteTimeSpan);
                    t++;
                }
            }

    主要用到了Timer类的Change方法。

    第一个参数表示:到下次执行需要的时间。为TimeSpan类型。

    第二个参数表示:永不过期。

  • 相关阅读:
    hdu 1715
    hdu 1370
    hdu 2370
    hdu 1393
    hdu 1564
    hdu 1720
    hdu 1342
    SQL技巧(sp_procedure_params_rowset,SQL中设置数据值为null)
    Control ‘dg’ of type 'GridView' must be placed inside a form tag with runat=server
    GridView事件中获取rowIndex值
  • 原文地址:https://www.cnblogs.com/subendong/p/12106493.html
Copyright © 2011-2022 走看看