TimeJob 定时任务框架
第一:引入NuGet包:
QCommon.TimeJob
在StartUp 中注册Job:
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddTimedJob(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseTimedJob(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); }); } }
编写执行类方法
public class SendMessageJob:Job { // Begin 起始时间;Interval执行时间间隔,单位是毫秒,建议使用以下格式,此处为3小时; //SkipWhileExecuting是否等待上一个执行完成,true为等待; [Invoke(Begin="2021-08-24 15:27",Interval =60*1000, SkipWhileExecuting = true)] public void Run() { Console.WriteLine($"当前时间{DateTime.Now}"); } }