Timed task-c# thread class Windows service

Source: Internet
Author: User

Principle

The most common is that the timer class in C # writes a timing method, and then hosts it to the Windows service.

Timer classification in C #

About the C # timer class in C # There are 3 of timer classes

C # Timer used by Method 1. Defined in System.Windows.Forms

C # Timer uses the Method 2. defined in the System.Threading.Timer class "

C # Timer used by Method 3. Defined in the System.Timers.Timer class

System.Windows.Forms.Timer

Applied to WinForm, it is implemented through the Windows Messaging mechanism, similar to the Timer control in VB or Delphi, implemented internally using API SetTimer. The main drawback is that the timing is imprecise and there must be a message loop that the console application (console application) cannot use.

System.Timers.Timer

Very similar to System.Threading.Timer, they are implemented through the. NET Thread pool, are lightweight, time-accurate, and have no special requirements for applications or messages.

System.Timers.Timer can also be applied to WinForm, completely replacing the timer control above. Their disadvantage is that they do not support direct drag-and-drop and require manual coding.

System.Threading.Timer

 Public classbizcommon{/// <summary>    ///Lock/// </summary>     Public Static ObjectLockObject =New Object();  Public Static voidStartTime () {//The second parameter is the parameter of the callback methodTimer T =NewTimer (Startbiz,NULL,0, the); //t.change (0, ();    }    Private Static voidStartbiz (Objecto) {if(Monitor.TryEnter (lockobject)) {FileStream fs=NewFileStream ("C:\\log.txt", FileMode.OpenOrCreate); StreamWriter SW=NewStreamWriter (FS); Sw. WriteLine ("Time :"+ DateTime.Now.ToString ("yyyy-mm-dd HH:mm:ss FFF")); Sw.        Close (); }    }}
View Code

System.Timers.Timer

 Public classbizcommon{/// <summary>    ///Lock/// </summary>     Public Static ObjectLockObject =New Object();  Public Static voidStartTime () {System.Timers.Timer TM=NewSystem.Timers.Timer (); Tm. Interval= the; Tm. Elapsed+=NewSystem.Timers.ElapsedEventHandler (startbiz); Tm. AutoReset=true;//performs a false once, and always loops execution trueTm. Enabled =true;//whether to execute the elapsed event. TM.        Start (); //TM. Stop ();    }    Private Static voidStartbiz (Objectsender, System.Timers.ElapsedEventArgs e) {        if(Monitor.TryEnter (lockobject)) {FileStream fs=NewFileStream ("C:\\log.txt", FileMode.OpenOrCreate); StreamWriter SW=NewStreamWriter (FS); Sw. WriteLine ("Time :"+ DateTime.Now.ToString ("yyyy-mm-dd HH:mm:ss FFF")); Sw.        Close (); }    }}
View Code

For more on multithreading tutorials please see

Http://www.cnblogs.com/wudequn/category/1154929.html

 Public Static voidStartTime () {System.Timers.Timer TM=NewSystem.Timers.Timer (); Tm. Interval= +; Tm. Elapsed+=NewSystem.Timers.ElapsedEventHandler (timer1_elapsed); Tm. Start ();}Private Static voidTimer1_elapsed (Objectsender, System.Timers.ElapsedEventArgs e) {    intInthour =E.signaltime.hour; intIntminute =E.signaltime.minute; intIntsecond =E.signaltime.second; //set the program to start every 00:00:00//    //due to computer performance problems, it is sometimes possible to skip a second, resulting in no execution. The exception that causes the task to be handled is called Missfire.//This is going to take a lot of processing, and other time periods to see if the database has been processed today. There is no processing on the re-processing and so on compensation operations.     if(Inthour = =xx&& Intminute = =xx&& Intsecond = =xx)    {        //Do something    }}
specific point-in-time processing tasks
Windows Services

Create a process

Download the project

After compiling, there are batch files installed and written in the Windows service.

Timed task-c# thread class Windows service

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.