一个任务可以绑定 多个触发器但是一个触发器不能绑定多个任务 使用委托实现多线程同步窗体
public partial class Form1 : Form { public static Form1 form1; public Form1() { InitializeComponent(); // System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; form1 = this; } public delegate void TextBoxAdd(string Str); //在给textBox1.text赋值的地方调用以下方法即可 public void SetStr(string Str) { // InvokeRequired需要比较调用线程ID和创建线程ID // 如果它们不相同则返回true if (this.textBox1.InvokeRequired) { TextBoxAdd d = new TextBoxAdd(SetStr); this.Invoke(d, new object[] { Str }); } else { textBox1.AppendText(Str + ""); } } private void Form1_Load(object sender, EventArgs e) { StdSchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = factory.GetScheduler().Result;// //指定具体做什么事儿,Job IJobDetail job = JobBuilder.Create<SendMessageJob>().Build(); IJobDetail job1 = JobBuilder.Create<SendMessageJob1>().Build(); job.JobDataMap.Add("student1", "落羽"); job.JobDataMap.Add("student2", "赤"); job.JobDataMap.Add("student3", "自由飞翔"); job.JobDataMap.Add("student4", "小刘"); job.JobDataMap.Add("Year", DateTime.Now.Year); //指定时间策略 // ITrigger trigger = TriggerBuilder.Create() //.WithCronSchedule // .WithSimpleSchedule(build => // { // build.WithIntervalInSeconds(3).RepeatForever(); //设置时间间隔,时分秒 // // //.WithMisfireHandlingInstructionFireNow() // // .WithRepeatCount(4); //设置执行次数,总共执行3+1次, // }) //.Build(); ITrigger trigger = TriggerBuilder.Create() .StartAt(DateTime.Now)//什么时候开始执行 // .WithIdentity("tname1", "group1") .WithDescription("高级班的QuartZ") //描述 .WithCronSchedule("0/5 * * * * ?") //从第三秒开始每五秒执行一次 .Build(); ITrigger trigger1 = TriggerBuilder.Create() .StartAt(DateTime.Now)//什么时候开始执行 // .WithIdentity("tname12", "group12") .WithDescription("高级班的QuartZ") //描述 .WithCronSchedule("0/6 * * * * ?") //从第三秒开始每五秒执行一次 .Build(); scheduler.ScheduleJob(job, trigger); scheduler.ScheduleJob(job1, trigger1); scheduler.Start(); } private void button1_Click(object sender, EventArgs e) { this.textBox1.Text = ""; } }View Code
public class SendMessageJob : IJob { /// <summary> /// 这里定义具体要做的事情 /// </summary> /// <param name="context"></param> /// <returns></returns> public Task Execute(IJobExecutionContext context) { return Task.Run(() => { //Action action = () => //{ // Form1.form1.textBox1.AppendText("你好senmesjon"); ; //}; //action.Invoke(); Form1.form1.SetStr("你好senmesjon"); Console.WriteLine("******************JobDetail.JobDataMap.Get******************"); Console.WriteLine(context.JobDetail.JobDataMap.GetString("student1")); Console.WriteLine(context.JobDetail.JobDataMap.GetString("student2")); Console.WriteLine(context.JobDetail.JobDataMap.GetString("student3")); Console.WriteLine(context.JobDetail.JobDataMap.GetString("student4")); Console.WriteLine(context.JobDetail.JobDataMap.GetInt("Year")); Console.WriteLine("这是要做的事情"+DateTime.Now); }); } }View Code
public class SendMessageJob1 : IJob { /// <summary> /// 这里定义具体要做的事情 /// </summary> /// <param name="context"></param> /// <returns></returns> public Task Execute(IJobExecutionContext context) { return Task.Run(() => { //Action action = () => //{ // Form1.form1.textBox1.AppendText("你好senmesjon111111"); ; //}; //action.Invoke(); Form1.form1.SetStr("你好senmesjon111111"); Console.WriteLine("******************JobDetail.JobDataMap.Get******************"); Console.WriteLine(context.JobDetail.JobDataMap.GetString("student1")); Console.WriteLine(context.JobDetail.JobDataMap.GetString("student2")); Console.WriteLine(context.JobDetail.JobDataMap.GetString("student3")); Console.WriteLine(context.JobDetail.JobDataMap.GetString("student4")); Console.WriteLine(context.JobDetail.JobDataMap.GetInt("Year")); Console.WriteLine("这是要做的事情"+DateTime.Now); }); } }View Code
标签:quartz,Console,context,winfrom,JobDataMap,窗体,WriteLine,public,JobDetail From: https://www.cnblogs.com/FanJunCheng521/p/17168162.html