Background program completes the specified task

Source: Internet
Author: User

The background performs the specified task mode:

SQL Job, Background WinForm program, Windows services, etc.;

There are pros and cons for each of the three;

This record WinForm the main code of the program performing background tasks:

1,quartz Way:

IScheduler sched =NULL; Private voidAutoupdateblack_load (Objectsender, EventArgs e) {            Try{ischedulerfactory SF=NewStdschedulerfactory ();//Performersched=SF.                Getscheduler (); Ijobdetail Job= Jobbuilder.create<upateblackhandler> (). Withidentity ("Job1","group1").                Build (); Icrontrigger Trigger=(Icrontrigger) triggerbuilder.create (). Withidentity ("Trigger1","group1")                                                        . Withcronschedule ("0 0 1 * *?")//run once every night at 1 o'clock                                                        .                Build (); Sched.                Schedulejob (Job, trigger); Sched.                Start (); Label1. Text="= = Program start = ="; Logclass. Debug ("Program Startup"); }            Catch(Exception ex) {Logclass. Debug ("program = = Error"+Ex.            ToString ()); }            NewUpateblackhandler (). Execute (NULL); }        Private voidAutoupdateblack_formclosed (Objectsender, Formclosedeventargs e) {            if(E.closereason = =closereason.windowsshutdown) {//Add the code you want to useLogclass. Debug (DateTime.Now +""The computer shuts down or is logged off"+"= = = System User:"+ System.Environment.UserName +"""); }            if(E.closereason = =closereason.taskmanagerclosing) {//Add the code you want to useLogclass. Debug (DateTime.Now +"the Task manager shuts down"+"= = = System User:"+ System.Environment.UserName +"""); }            if(E.closereason = =closereason.none) {//Add the code you want to useLogclass. Debug (DateTime.Now +""Unknown unexpected shutdown"+"= = = System User:"+ System.Environment.UserName +"""); }            if(Sched! =NULL) {sched. Shutdown (false); }        }        Private voidAutoupdateblack_formclosing (Objectsender, FormClosingEventArgs e) {Logclass. Debug ("= = Program off = =");        System.Environment.Exit (System.Environment.ExitCode); }
View Code

Reference: http://www.cnblogs.com/mushroom/p/4067037.html

Cron Expressions use Format (http://www.thinksaas.cn/group/topic/99159/)

Seconds Minutes Hours DayOfMonth Month DayofWeek [Year]
Seconds Score of When Days Month Week Years

Each symbol represents the meaning:

    1. *: Matches any value of the field, such as * used in the field of the branch, indicating that the event will be triggered every minute.
    2. ?: matches any value of the field.
    3. -: Matches a specific range value, such as when the value of the field is 10-12, indicating that 10, 11, 12 points will trigger the event.
    4. ,: Matches multiple specified values, such as the value of the field where the week is 2,4,6, indicating that events are triggered in Monday, Wednesday, Friday (1 for Sunday, 2 for Monday, 3 for Tuesday, and so on, 7 for Saturday).
    5. /: The left is the start trigger time, on the right is the event triggered every fixed time, such as the second field of the value is 5/15, indicating 5 seconds, 20 seconds, 35 seconds, 50 seconds to trigger an event.
    6. L:last, last meaning, if it is used on the day of this field, represents the last day of the month, if it is used in the domain of the week, such as 6L, represents the last Friday of a month. (Foreign Sunday is Star Yiu Day, Monday is the month Yao Day, the beginning of the week is Sunday, so 1l= Sunday, 6l= Friday.) )
    7. W:weekday, the meaning of the workday. If the value of the field is 15W, indicating the most recent working day of 15th this month, if 15th is Saturday, the trigger will trigger on 14th Friday. If 15th is Sunday, triggers will trigger 16th Monday. If 15th is not Saturday or Sunday, but one from Monday to Friday, then it triggers the event on 15th.
    8. #: Used to specify the number of weeks of the month, as 6#3 represents the third Friday of a month.

Practical examples of expression meaning

"0 0 12 * *?" Trigger events every day 12:00
"0 15 10?" * *” Trigger events every day 10:15
"0 15 10 * *?" Trigger events every day 10:15
"0 15 10 * *?" *” Trigger events every day 10:15
"0 15 10 * *?" 2005″ 2005 Daily 10:15 Trigger event
"0 * 14 * *?" Triggers every 14 o'clock, triggers once per minute, ends 14:59 minutes
"0 0/5 14 * *?" Triggers an event every 5 minutes from 14 o'clock onwards to 14:59 minutes
"0 0/5 14,18 * *?" Events are triggered every 5 minutes from 14 o'clock to 14:59 and 18 to 18:59 every day
"0 0-5 14 * *?" Events are triggered every 1 minutes from 14 o'clock to 14:05 every day
"0 10,44 14?" 3 WED " 14:10 and 14:44 per year in March of Wednesday trigger an event
"0 15 10?" * Mon-fri " Monday to Friday 10:15 triggers an event
"0 15 10 15 *?" Monthly 15th 10:15 triggers an event
"0 L *?" 10:15 of the last day of the month triggers an event
"0 15 10?" * 6L " The last month of Friday 10:15 triggers an event
"0 15 10?" * 6L 2002-2005″ From 2002 to 2005, the last Friday 10:15 triggers an event
"0 15 10?" * 6#3″ The third Friday 10:15 of the month triggers an event

2, Thread pool background execution (ThreadPool.QueueUserWorkItem)

        Private voidForm1_Load (Objectsender, EventArgs e) {Label1. Text="= = Program start = ="; Label1. Text+="\ r \ n Platform is started"; Hangdler HD1=NewHangdler (); ThreadPool.QueueUserWorkItem (NewWaitCallback (HD1. INTODB),"1"); }       //Hangdler Main code: Public voidIntodb (Objectobj) {         stringcanshu=obj.        ToString ()); //Todo}
View Code

Background program completes the specified task

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.