Magic Timer, magic Timer

Source: Internet
Author: User

Magic Timer, magic Timer

In a recent project, timing functions are needed in some places. during the design process, I suddenly found that the. net Timer class still has many features that I have never used before. I will share them with you here.

Note: The Timer class here refers to the System. Threading. Timer class.

Scenario 1: The server needs to perform an operation at every day.

When I first thought of 2b method, I set the timer to check the time every one minute. If the difference between the current time and is no more than 1 minute, I will execute the operation !!! Because this code is too 2b, we will not put it on!

I don't even know how I thought of such a 2 design at the beginning. I just wrote the code and I myself denied this solution, which is a waste of resources, you cannot perform operations accurately!

So I checked msdn again. msdn is really a programming artifact. Now I leave it, and it is almost impossible. I was pleasantly surprised to find that there is such a parameter in the Timer constructor.

Isn't the dueTime parameter exactly what I need? However, I used to ignore this parameter and set it to 0! So I immediately wrote the following code

var span = DateTime.Today.AddDays(1) - DateTime.Now; var timer = new Timer(callback, null, (int)span.TotalMilliseconds, 24 * 3600 * 1000);

So at the next day, the timer starts to execute and then runs every 24 hours. This perfectly achieves the goal in the scenario!

Scenario 2: I need to save the content in RichTextBox every 5 minutes.

This is actually an automatic saving function similar to Word. At the beginning, I thought it was very simple. Isn't it just about defining a 5-minute timer? Later I thought it was not that easy, because if the user does not change anything in RichTextBox, it would be a waste of resources to save it every five minutes! Therefore, we can describe the preceding scenario more accurately as follows:

Save the content in RichTextBox once every 5 minutes after you modify the content of RichTextBox!

How can we achieve this? So I checked MSDN again, but it didn't disappoint me. MSDN once again surprised me. Please refer to the following MSDN description.

This is just to solve the problem in the situation! So I immediately wrote the following code:

System.Threading.Timer timer = null;        bool needSave = false;        private void richTextBox1_TextChanged(object sender, EventArgs e)        {            if (timer == null)                timer = new System.Threading.Timer(Callback, null, 5 * 60 * 1000, 0);            else if (needSave)            {                needSave = false;                timer.Change(5 * 60 * 1000, 0);            }        }        void Callback(object state)        {               Save();            needSave = true;        }
Setting the period parameter to 0 means that the timer will only be executed once. At this time, the timing function is implemented by the dueTime parameter. The Change function can restart the timing function, the timer will be reset every time there is a change in the content in RichTextBox, which perfectly solves the problem in the situation!

 

Provided by "blog Park"

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.