.Net Core控制台应用程序使用定时器
【摘要】
引用两个库:
McApp.QuartzCore
Pomelo.AspNetCore.TimedJob
using Newtonsoft.Json;using Quartz;using Quartz.Impl;using System;using System.Collections.Specialized;using Sy...
引用两个库:
McApp.QuartzCore
Pomelo.AspNetCore.TimedJob
using Newtonsoft.Json;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Specialized;
using System.Threading.Tasks;
namespace TestDockerConsole
{
public class HelloJob:IJob{
public Task Execute(IJobExecutionContext context) {
return Console.Out.WriteLineAsync("Greetings from HelloJob!");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Time Job Start");
RunProgram().GetAwaiter().GetResult();
Console.WriteLine("Hello World!");
Console.Read();
}
private static async Task RunProgram()
{
try
{
// Grab the Scheduler instance from the Factory
NameValueCollection props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
IScheduler scheduler = await factory.GetScheduler();
// 启动任务调度器
await scheduler.Start();
// 定义一个 Job
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("job1", "group1")
.Build();
ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
.WithIdentity("trigger1") // 给任务一个名字
.StartAt(DateTime.Now) // 设置任务开始时间
.ForJob("job1", "group1") //给任务指定一个分组
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(1) //循环的时间 1秒1次
.RepeatForever())
.Build();
// 等待执行任务
await scheduler.ScheduleJob(job, trigger);
// some sleep to show what's happening
//await Task.Delay(TimeSpan.FromMilliseconds(2000));
}
catch (SchedulerException se)
{
await Console.Error.WriteLineAsync(se.ToString());
}
}
}
}
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/79276569
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)