添加项目文件。
This commit is contained in:
67
WPFWCSDemo/Common/CustomAttributes/TimerTaskAttribute.cs
Normal file
67
WPFWCSDemo/Common/CustomAttributes/TimerTaskAttribute.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WPFWCSDemo.Common.CustomAttributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class TimerTaskAttribute:Attribute
|
||||
{
|
||||
public int Second { get; set; }
|
||||
public int Minute { get; set; }
|
||||
public int Hour { get; set; }
|
||||
|
||||
private IScheduler _scheduler;
|
||||
|
||||
/// <summary>
|
||||
/// Quartz调度任务注册
|
||||
/// </summary>
|
||||
/// <param name="className">类名</param>
|
||||
/// <param name="jobName">作业名称</param>
|
||||
/// <param name="triggerName">触发器名称</param>
|
||||
/// <param name="group">任务组名称</param>
|
||||
private async void GetScheduleJob(Type className, string jobName, string triggerName, string group, int time)
|
||||
{
|
||||
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
|
||||
_scheduler = schedulerFactory.GetScheduler().Result;
|
||||
await _scheduler.Start();
|
||||
|
||||
var method = typeof(JobBuilder)
|
||||
.GetMethods()
|
||||
.First(m => m.Name == "Create" && m.IsGenericMethod);
|
||||
|
||||
var genericMethod = method.MakeGenericMethod(className);
|
||||
var jobBuilder = genericMethod.Invoke(null, null);
|
||||
|
||||
//任务处理---注册任务
|
||||
IJobDetail job = ((JobBuilder)jobBuilder)
|
||||
//.Create<className>()
|
||||
.WithIdentity(jobName, group)
|
||||
.DisallowConcurrentExecution()
|
||||
.Build();
|
||||
|
||||
//任务处理---触发器
|
||||
ITrigger trigger = TriggerBuilder
|
||||
.Create()
|
||||
.WithIdentity(triggerName, group)
|
||||
.StartNow()
|
||||
.WithSimpleSchedule(x => x
|
||||
.WithInterval(TimeSpan.FromMilliseconds(time))
|
||||
.RepeatForever())
|
||||
.Build();
|
||||
|
||||
await _scheduler.ScheduleJob(job, trigger);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user