zoukankan      html  css  js  c++  java
  • .net core控制台基于backgroundService后台任务实现的指定时间运行的定时器

    简单的实现了一个.net core控制台基于backgroundService后台任务实现的指定时间运行的定时器,到了第二天也会默认启动此时间点,可自定义配置多个时间段,多线程运行。话不多说,直接帖代码。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <!--当天的某一点时间,可自己手动设置多段启动,也可手动删除,第二天到了时间点会自动默认启动-->

    <!--时间格式: 小时:分钟-->
    <add key="Time" value="11:51"></add>
    <add key="Time1" value="11:52"></add>
    <add key="Time2" value="12:10"></add>
    </appSettings>
    </configuration>
    这里配置文件先配置好你要哪个时间点启动任务,可自己设置时间,删除时间。
    然后获取配置里面的对应的值

    public class CommenHelper
    {
    /// <summary>
    /// 根据索引获取配置文件的值
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static string GetConfigTime(int index)
    {
    return ConfigurationManager.AppSettings.Get(index);
    }
    /// <summary>
    /// 获取配置文件时间段数量
    /// </summary>
    /// <returns></returns>
    public static int GetConfigTimeCount()
    {
    return ConfigurationManager.AppSettings.Count;
    }
    }

    这两个方法先定义好,后面会用到,

    public class TimeBackgroundService : BackgroundService
    {
    //服务一启动就会执行此方法
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
    Console.WriteLine("Execute执行!");
    if (!stoppingToken.IsCancellationRequested)
    {
    await Task.Run(() => SetAllTaskTime());
    }
    }

    public override async Task StopAsync(CancellationToken stoppingToken)
    {
    await Task.Run(() => { Console.WriteLine("已注销停止服务!"); });
    }
    /// <summary>
    /// 业务逻辑实现
    /// </summary>
    /// <param name="state"></param>
    private void DoWork(object state)
    {
    Console.WriteLine("业务逻辑开始执行一次。。。。。");
    Thread.Sleep(120000);
    Console.WriteLine("业务逻辑开始执行一次完成。。。。。");
    }
    /// <summary>
    /// 循环配置文件里面多个时间实现定时器
    /// </summary>
    private void SetAllTaskTime()
    {
    var count = CommenHelper.GetConfigTimeCount();
    for (int i = 0; i < count; i++)
    {
    DateTime now = DateTime.Now;
    DateTime oneOClock = DateTime.Today.AddHours(Convert.ToDouble(CommenHelper.GetConfigTime(i).Split(":")[0]))
    .AddMinutes(Convert.ToDouble(CommenHelper.GetConfigTime(i).Split(":")[1]));//设置运行时间
    //如果当前时间大于设置时间,即第二天这个时间在启动
    if (now > oneOClock)
    {
    oneOClock = oneOClock.AddDays(1.0);
    }
    int msUntilFour = (int)((oneOClock - now).TotalMilliseconds);
    Console.WriteLine(oneOClock + "开始执行。。。。。");
    var t = new System.Threading.Timer(DoWork);
    t.Change(msUntilFour, Timeout.Infinite);
    }
    }
    }

    在DoWork()方法里面实现要指定时间启动的任务逻辑即可。
    定时器用的是System.Threading.Timer里面的timer。不祥的可以去查资料。

    在再program.cs类里面注入刚才定义的TimeBackgroundService服务。

    static void Main(string[] args)
    {
    var build = new HostBuilder()
    .ConfigureServices(option =>
    {
    option.AddHostedService<TimeBackgroundService>();
    });

    build.RunConsoleAsync();

    Console.ReadLine();
    }
    }

    以上代码即为一个简单的后台定时任务。
    原文链接:https://blog.csdn.net/ZhaoHuaQiao_FL/article/details/106947099

  • 相关阅读:
    IOSUITextField类
    IOSUITableView设置背景图片,方式与众不同。
    IOS图标知识详情(转载自http://luoyl.info/blog/2012/03/iphoneipadicons/)
    IOSCreate UIActionSheet 'otherButtons' by passing in array
    Offset文件名(绝对偏移量)
    单例模式(Singleton)Holder
    在 spring 中一共定义了七种事务传播属性
    UML,各种关系合集
    Java,线程池,ThreadPoolExecutor
    EasyMock
  • 原文地址:https://www.cnblogs.com/wl-blog/p/15097576.html
Copyright © 2011-2022 走看看