第一步:Install-Package Quartz
namespace ProjectEdb
{
class Program
{
static void Main(string[] args)
{
//例子1
QuartzJob.CreateJob("test", "zkb", "0 */1 * * * ?").GetAwaiter().GetResult();
//例子2
//RunProgram().GetAwaiter().GetResult();
}
}
}
namespace ProjectEdb.Models
{
public class QuartzJob
{
public static async Task CreateJob(string name, string group, string cron)
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = await schedulerFactory.GetScheduler();
DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now, );
DateTimeOffset endTime = DateBuilder.NextGivenMinuteDate(DateTime.Now, );
IJobDetail job = JobBuilder.Create<EdbQuartz>()
.WithIdentity(name, group)
.Build();
ICronTrigger cronTrigger = (ICronTrigger)TriggerBuilder.Create()
.StartAt(startTime)
.EndAt(endTime)
.WithIdentity(name, group)
.WithCronSchedule(cron)
.Build();
await scheduler.ScheduleJob(job, cronTrigger);
await scheduler.Start();
Console.ReadLine();//这句话在net core控制台应用程序里是一定要加上的,否则,控制台的输出框会闪一下就没了
}
}
}
namespace ProjectEdb.Models
{
public class EdbQuartz: IJob
{
public async Task Execute(IJobExecutionContext context)
{
string path = @"D:\EDBStockInfo_Files\2019\1.txt";
string value = DateTime.Now.ToString();
if (File.Exists(path))
{
//File.Delete(path);
}
StreamWriter streamWriter = new StreamWriter(path, true);
await streamWriter.WriteLineAsync(value);
streamWriter.Flush();
streamWriter.Close();
}
}
}