Write and register the Windws Service in the. NET environment

Source: Internet
Author: User
Tags datetime
/* ===================================================== ====================================
Although it is simple, we seldom write such an application. Once in a while, I can easily extract this article.
Writing and Publishing simple Windows services on the. NET platform is very convenient.
Create a (VB. NET/C #) project, select Windows Service application... then you can add code in the OnStart class.
If you want to implement the scheduled operation and live real-time listening function, you can use the TIMER class provided by. NET.
The following is a code snippet.
========================================================== ===================================== */
Private void theTimer_Elapsed (object sender, System. Timers. ElapsedEventArgs e)
{If (! ExCondition ())
{Return;
}
Try
{TheTimer. Enabled = false;
MyTransaction ();
}
Catch (Exception es)
{
WriteLog (es. Message );
}
TheTimer. Enabled = true;
}
// Check execution conditions
Private bool ExCondition ()
{
DateTime dtNow = DateTime. Now;
If (this. currentmode. Equals ("auto "))
{

String [] frev = this. freqvalue. Split (''-'');

Switch (this. freq)
{
Case "2": // 2-hourly
If (dtNow. Minute = 59) & (dtNow. Second <30 ))
{
Return true;
}
Break;
Case "4": // 4-daily,
If (dtNow. hour = int. parse (frev [1]) & (dtNow. minute = int. parse (frev [2]) & (dtNow. second <30 ))
{
Return true;
}
Break;
Case "8": // 8-weekly,
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.