Timer usage in Windows Service

Source: Internet
Author: User

When you use C # To write Windows Services, many examples on the Internet say that you can use the timer in the Toolkit, but in fact this timer does not work, for specific reasons, microsoft claimed to be a framework bug.

Both timer in Windows form and timer in components are inherited from system. windows. form. timer, not the system we need. timers, inherited from system. timers can only be created manually.

Public windowsservicedemo ()
{
Initializecomponent ();
System. Timers. Timer T = new system. Timers. Timer (1000); // instantiate the Timer class and set the interval to 10000 milliseconds;
T. elapsed + = new system. Timers. elapsedeventhandler (timeelapse); // execute the event at the time of arrival;
T. autoreset = true; // set whether to execute once (false) or always execute (true );
T. Enabled = true; // whether to execute the system. Timers. Timer. elapsed event;
}
Public void timeelapse (Object source, system. Timers. elapsedeventargs E)
{
// EventLog log = new EventLog ();
// Log. Source = "My applications ";
// Log. writeentry ("one second call", eventlogentrytype. information );
Filestream FS = new filestream (@ "D: \ timetick.txt", filemode. openorcreate, fileaccess. Write );
Streamwriter m_streamwriter = new streamwriter (FS );
M_streamwriter.basestream.seek (0, seekorigin. End );
M_streamwriter.writeline ("after one second" + datetime. Now. tostring () + "\ n ");
M_streamwriter.flush ();
M_streamwriter.close ();
FS. Close ();
 
}
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.