C # scheduled execution,

Source: Internet
Author: User

C # scheduled execution,

Code:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. IO; using System. linq; using System. text; using System. threading; using System. threading. tasks; using System. windows. forms; using Common. utils; namespace test {public partial class Form1: form {/// <summary> /// file Path that records the last execution time /// </summary> private string lastRunTimePath = Path. C Ombine (Application. startupPath, "lastRunTime.txt"); // <summary> // last execution time, accurate to seconds /// </summary> private string lastRunTime {get {if (File. exists (lastRunTimePath) {using (StreamReader sr = new StreamReader (lastRunTimePath) {string result = sr. readToEnd (); sr. close (); return result ;}} return null;} set {if (! File. exists (lastRunTimePath) {File. create (lastRunTimePath);} using (StreamWriter sw = new StreamWriter (lastRunTimePath, false) {sw. write (value); sw. close () ;}} public Form1 () {InitializeComponent () ;}private void button#click (object sender, EventArgs e) {System. windows. forms. timer timer = new System. windows. forms. timer (); timer. interval = 50; // the scheduled execution time is precise to the second, so the time Interval between Timer is smaller than 1 second timer. tick + = New EventHandler (a, B) => {DateTime now = DateTime. Now; if (now. ToString ("yyyy-MM-dd HH: mm: ss ")! = LastRunTime) // if it is equal, it indicates that {lastRunTime = now has been executed. toString ("yyyy-MM-dd HH: mm: ss"); LogUtil. log (now. toString ("yyyy-MM-dd HH: mm: ss. fff ") +" the time is up, execute the task "); // TODO: Execute the task here}); timer. start ();}}}View Code

Note:

1. Why should I save lastRunTime to a file?

This is to ensure that when the program restarts, the time can still be correctly determined and will not be repeated.

2. What is the difference between the implementation of scheduled tasks in Windows?

This is also a disadvantage of this implementation method. The precision is not high enough. If the scheduled task is scheduled to minutes, the precision is generally only to minutes. It can be applied to scheduled tasks with low precision. Although reducing Interval can improve the timing precision, it consumes CPU resources.

3. Why is it okay to directly judge the hour, minute, and second?

It takes time for the program to run, even if the time is very short, it will lead to skipping a certain time. For example, the Interval is set to 1000, that is, 1 second, and it is executed once at, 50 minutes, And 29.999 seconds, the next Judgment time reaches 13: 50: 31.001 seconds, and the time is skipped from 13: 50: 30 seconds. If the number of seconds is equal to the value of the judgment, it will not be executed.

4. This is only a test program. It needs to be modified according to the actual situation. For example, it can be set from the time of the configuration file to the minute, then, judge whether the time is reached by the method in the program.

Test log:

 

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.