Scheduled tasks: In my personal understanding, the background control program requires that the corresponding tasks be executed at a certain time.
Directly run the Code:
Previously, because of work requirements, we required to execute the data to be done at every day.
1.1 declare an interface ijob
Method: Run
Public interface ijob {// execution method void execute ();}
1.2 declare a Scheduler configuration class
Attribute: task list (task to be executed), interval (interval );
/// <Summary> /// Scheduler configuration /// </Summary> public class schedulerconfiguration {private int sleepinterval; private list <ijob> jobs = new list <ijob> (); Public schedulerconfiguration (INT newsleepinterval) {This. sleepinterval = newsleepinterval;} // <summary> // task list // </Summary> public list <ijob> jobs {get {return jobs ;}} /// <summary> /// time interval /// </Summary> Public int sleepinterval {get {return sleepinterval;} set {sleepinterval = value ;}}
1.3 declare a scheduler
Method: Enable
Attribute: scheduling program configuration object
/// <Summary> // scheduler // </Summary> public class scheduler {private schedulerconfiguration configuration = NULL; Public scheduler (schedulerconfiguration schedulerconn) {sconn = schedulerconn ;} /// <summary> /// Method for starting execution /// </Summary> Public void start () {While (true) {foreach (ijob job in configuration. jobs) {threadstart _ threaddelegate = new threadstart (job. execute); thread _ thread = new thread (_ threaddelegate); _ thread. start (); thread. sleep (configuration. sleepinterval) ;}}/// <summary> /// Scheduler configuration information /// </Summary> Public schedulerconfiguration sconn {get {return configuration ;} set {configuration = value ;}}
1.4 Implementation: ijob Interface
class person : IJob { void IJob.Execute() { ThreadTest(); } public void ThreadTest(){ //...... } }
1.5 run in the program entry (main thread)
Public static system. threading. thread schedulerthread = NULL; static void main (string [] ARGs) {// Scheduler configuration (executed every hour) schedulerconfiguration Config = new schedulerconfiguration (1000*60*60 ); // Add a Config for the configuration. jobs. add (new person (); // scheduler = new scheduler (config); // execution thread schedulerthread = new system. threading. thread (New threadstart (schedstart. start); schedulerthread. start ();}
In this way, a simple task is planned.