麦田

不积跬步无以至千里.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
public partial class Form1 : Form
{
    StdSchedulerFactory factory;
    IScheduler scheduler;
    public Form1()
    {
        InitializeComponent();
        MouseControl.OnEventLog += MouseControl_OnEventLog;
        Control.CheckForIllegalCrossThreadCalls = false;//取消线程间的安全检查
        
        // 工厂
        factory = new StdSchedulerFactory();
        //创建任务调度器
        scheduler = factory.GetScheduler().Result;
    }

    //显示执行记录的方法
    private void MouseControl_OnEventLog(string message)
    {
        this.textBoxLog.Text += DateTime.Now.ToString("HH:mm:ss.fff") + ":" + message + "\r\n";
    }

    /// <summary>
    /// 时间格式转换成Quartz任务调度器Cron表达式
    /// </summary>
    /// <param name="time">时间值,支持HH:mm:ss | HH:mm</param>
    /// <returns></returns>
    public static string TimeToQuartzCron(string time)
    {
        try
        {
            if (string.IsNullOrWhiteSpace(time)) return "";
            string error = "传入的时间值[" + time + "]格式有误!";
            int ss = 0, mi = 0, hh = 0;
            if (time.Length < 5) throw new Exception(error);
            if (time.Substring(2, 1) != ":") throw new Exception(error);

            if (!int.TryParse(time.Substring(0, 2), out hh))
                throw new Exception(error);
            if (!int.TryParse(time.Substring(3, 2), out mi))
                throw new Exception(error);
            if (time.Length > 5)
            {
                if (time.Substring(5, 1) != ":") throw new Exception(error);
                if (!int.TryParse(time.Substring(6), out ss))
                    throw new Exception(error);
            }
            if (ss > 59) throw new Exception(error);
            if (mi > 59) throw new Exception(error);
            if (hh > 23) throw new Exception(error);
            string cronValue = ss + " " + mi + " " + hh + " " + "* * ?";
            return cronValue;
        }
        catch (Exception ea)
        {
            throw ea;
        }
    }

    string triggerKey = "TaobaoJobTrigger";
    string jobName = "TaobaoJob";
    private void button1_Click(object sender, EventArgs e)
    {
        //取消之前的任务
        scheduler.PauseTrigger(new TriggerKey(triggerKey));// 停止触发器
        scheduler.UnscheduleJob(new TriggerKey(triggerKey));// 移除触发器
        scheduler.DeleteJob(JobKey.Create(jobName));

        //启动任务调度器
        scheduler.Start();

        //创建一个工作
        IJobDetail job = JobBuilder.Create<TaobaoJob>()
         .WithIdentity(jobName)
         .UsingJobData("FirstTime", this.textBoxFirstTime.Text) //传参
         .UsingJobData("SecondTime", this.textBoxSecondTime.Text)
         .UsingJobData("RelayMs", Convert.ToInt32(this.textBoxRelayMs.Text))
         .UsingJobData("IntervalMs", Convert.ToInt32(this.textBoxIntervalMs.Text))
        .Build();

        //创建一个触发条件
        ITrigger trigger = TriggerBuilder.Create()
           .WithIdentity(triggerKey)
           .WithCronSchedule(TimeToQuartzCron(this.dateTimePicker1.Text))
           .Build();

        //将创建的任务和触发器条件添加到创建的任务调度器当中
        scheduler.ScheduleJob(job, trigger);

        this.textBoxLog.Text = string.Empty;
        MouseControl_OnEventLog("任务创建");
    }


    private void button2_Click(object sender, EventArgs e)
    {
        //取消之前的任务
        scheduler.PauseTrigger(new TriggerKey(triggerKey));// 停止触发器
        scheduler.UnscheduleJob(new TriggerKey(triggerKey));// 移除触发器
        scheduler.DeleteJob(JobKey.Create(jobName));
    }
}
public class TaobaoJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        JobDataMap dataMap = context.JobDetail.JobDataMap;
        string firstTime = dataMap.GetString("FirstTime");
        string secondTime = dataMap.GetString("SecondTime");
        int relayMs = dataMap.GetInt("RelayMs");
        int intervalMs = dataMap.GetInt("IntervalMs");
      
        //执行的任务

        MouseControl.ShowMessage("执行完毕");
    }
}

 

posted on 2025-02-17 11:22  一些记录  阅读(44)  评论(0)    收藏  举报