Stop the Service (tips for using timer in Windows Service)

Source: Internet
Author: User

During development, a common requirement is to develop a backendProgramTo monitor the changes of some data in the database and immediately respond to the changes.

In fact, we will create a Windows Service Project, put a timer component in it, and then regularly connect to the database, judge, and then perform operations.

This is a small program made in the past two days, but every few days, the service will find that the service does not know when, and it will be inexplicably stopped, find the program, and no error is found. So I looked for the log and found that the database server updated the program at that time and restarted automatically. Because my Windows service connects to the database once every one second, it will certainly encounter an sqlexception exception and will naturally go down.

After determining the cause of the problem, the solution is clear. When the database is normal, it connects to the database every one second. Once it is found that the database cannot be connected, it is changed to 5 minutes or longer to connect to the database until the database can be used normally, modify the interval to 1 second. Most importantly, Timer component events cannot be interrupted no matter what happens to the database.

Specific implementation:
1. Drag the toolbox to the timer of the window, which is actually system. windows. forms. timer, this guy does not trigger the event in Windows Service, and the extension is designer. in the CS file, change it to system. timers. timer.
2. In the properties of the control or in the onstart event of the service, set the Enable of timer to true, and interval to the value of the database to be rotated. The unit is milliseconds.
3. The next step is the timer event. refer to the following section.CodeOK.

Private   Void Timersponelapsed ( Object Sender, system. Timers. elapsedeventargs E)
{
Timer1.enabled =   False ;

Try
{
// Operate the database here

Timer1.interval =   1   *   1000 ;
}
Catch (Sqlexception)
{
Timer1.interval =   5   *   60   *   1000 ;
}
Finally
{
Timer1.enabled =   True ;
}
}

It is worth noting that it takes time to throw an exception, so although this example is set to 5 minutes, the round robin actually takes a little longer than 5 minutes.

The above only processes database exceptions. In fact, the exception can be bypassed. If Windows Service is used for other purposes, it may be temporarily interrupted but will be restored later, can be processed in a similar way. For example, network interruption.

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.