后台程序完成指定任务

后台执行指定任务方式:

sql Job,后台winform程序,windows服务等;

就三者而言各有利弊;

此次记录winform程序执行后台任务的主要代码:

1,Quartz方式:

IScheduler sched = null;
        private void AutoUpdateBlack_Load(object sender, EventArgs e)
        {
            try
            {
                ISchedulerFactory sf = new StdSchedulerFactory();//执行者  
                
                sched = sf.GetScheduler();

                IJobDetail job = JobBuilder.Create<UpateBlackHandler>().WithIdentity("job1", "group1").Build();

                ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                                        .WithIdentity("trigger1", "group1")
                                                        .WithCronSchedule("0 0 1 * *  ?") //每天晚上1点运行一次
                                                        .Build();

                sched.ScheduleJob(job, trigger);
                sched.Start();

                label1.Text = "==程序启动==";
                logclass.Debug("程序启动");
            }
            catch (Exception ex)
            {
                logclass.Debug("程序==错误" + ex.ToString());
            }
            new UpateBlackHandler().Execute(null);

        }

        private void AutoUpdateBlack_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (e.CloseReason == CloseReason.WindowsShutDown)
            {
                //添加所需使用的代码
                logclass.Debug(DateTime.Now + "【电脑关机或者被注销" + "===系统用户:" + System.Environment.UserName + "");
            }
            if (e.CloseReason == CloseReason.TaskManagerClosing)
            {
                //添加所需使用的代码
                logclass.Debug(DateTime.Now + "【任务管理器关闭" + "===系统用户:" + System.Environment.UserName + "");
            }
            if (e.CloseReason == CloseReason.None)
            {
                //添加所需使用的代码
                logclass.Debug(DateTime.Now + "【未知意外关闭" + "===系统用户:" + System.Environment.UserName + "");
            }

            if (sched != null)
            {
                sched.Shutdown(false);
            }
        }

        private void AutoUpdateBlack_FormClosing(object sender, FormClosingEventArgs e)
        {
            logclass.Debug("==程序关闭==");
            System.Environment.Exit(System.Environment.ExitCode);
        }
View Code
UpateBlackHandler:
//[DisallowConcurrentExecution] //不允许此 Job 并发执行任务(禁止新开线程执行)
    public class UpateBlackHandler : IJob
    {

        public void Execute(IJobExecutionContext context)
        {
            this.UpdateBlack();
        }
        public void UpdateBlack()
        {
           //todo;
        }
    }
View Code

参考:http://www.cnblogs.com/mushroom/p/4067037.html

 dll:Quartz.NET 2.3.2

Cron表达式生成器:http://www.cnblogs.com/yanweidie/p/3537144.html

Cron表达式使用格式(http://www.thinksaas.cn/group/topic/99159/)

Seconds Minutes Hours DayofMonth Month DayofWeek [Year]
[年]

每个符号代表的含义:

  1. *:匹配该域的任意值;如*用在分所在的域,表示每分钟都会触发事件。
  2. ?:匹配该域的任意值。
  3. -:匹配一个特定的范围值;如时所在的域的值是10-12,表示10、11、12点的时候会触发事件。
  4. ,:匹配多个指定的值;如周所在的域的值是2,4,6,表示在周一、周三、周五就会触发事件(1表示周日,2表示周一,3表示周二,以此类推,7表示周六)。
  5. /:左边是开始触发时间,右边是每隔固定时间触发一次事件,如秒所在的域的值是5/15,表示5秒、20秒、35秒、50秒的时候都触发一次事件。
  6. L:last,最后的意思,如果是用在天这个域,表示月的最后一天,如果是用在周所在的域,如6L,表示某个月最后一个周五。(外国周日是星耀日,周一是月耀日,一周的开始是周日,所以1L=周日,6L=周五。)
  7. W:weekday,工作日的意思。如天所在的域的值是15W,表示本月15日最近的工作日,如果15日是周六,触发器将触发上14日周五。如果15日是周日,触发器将触发16日周一。如果15日不是周六或周日,而是周一至周五的某一个,那么它就在15日当天触发事件。
  8. #:用来指定每个月的第几个星期几,如6#3表示某个月的第三个星期五。

实用的例子 表达式 含义

“0 0 12 * * ?” 每天12:00触发事件
“0 15 10 ? * *” 每天10:15触发事件
“0 15 10 * * ?” 每天10:15触发事件
“0 15 10 * * ? *” 每天10:15触发事件
“0 15 10 * * ? 2005″ 2005年的每天10:15触发事件
“0 * 14 * * ?” 每天14点开始触发,每分钟触发一次,14:59分结束
“0 0/5 14 * * ?” 每天14点开始触发到14:59分结束的每5分钟触发一次事件
“0 0/5 14,18 * * ?” 每天14点开始到14:59期间和18点到18:59期间的每5分钟触发一次事件
“0 0-5 14 * * ?” 每天14点到14:05期间的每1分钟触发一次事件
“0 10,44 14 ? 3 WED” 每年3月的星期三的14:10和14:44触发一次事件
“0 15 10 ? * MON-FRI” 周一至周五的10:15触发一次事件
“0 15 10 15 * ?” 每月15日10:15触发一次事件
“0 15 10 L * ?” 每月最后一日的10:15触发一次事件
“0 15 10 ? * 6L” 每月的最后一个星期五10:15触发一次事件
“0 15 10 ? * 6L 2002-2005″ 2002年至2005年的每月的最后一个星期五10:15触发一次事件
“0 15 10 ? * 6#3″ 每月的第三个星期五10:15触发一次事件

2,线程池后台执行(ThreadPool.QueueUserWorkItem)

        private void form1_Load(object sender, EventArgs e)
        {

            label1.Text = "==程序启动==";
            label1.Text += "
平台已启动";
            Hangdler hd1 = new Hangdler();
            ThreadPool.QueueUserWorkItem(new WaitCallback(hd1.IntoDB), "1");
        }

       //hangdler 主要代码:
public void IntoDB(object obj)
        {
         string   canshu= obj.ToString());
        //todo 
        }
View Code
原文地址:https://www.cnblogs.com/systemkk/p/5257534.html