C # original tasks of Ming Xuan

Source: Internet
Author: User

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.

 

Related Article

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.