Write and publish simple Windows Services on the. NET platform

Source: Internet
Author: User

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,

Int we = int. parse (frev [0]. substring (1, 1 ));

If (dtnow. dayofweek. tostring (). equals (week [we]) & (dtnow. hour = int. parse (frev [1]) & (dtnow. minute = int. parse (frev [2]) & (dtnow. second <30 ))

{

Return true;

}

Break;

Case "16": // 16-monthly,

Int Mm = int. parse (frev [0]. substring (1, frev [0]. Length-1 ));

If (dtnow. day = mm) & (dtnow. hour = int. parse (frev [1]) & (dtnow. minute = int. parse (frev [2]) & (dtnow. second <30 ))

{

Return true;

}

Break;

Default: // No trigger

Return true;

Break;
}

}

Else

{

Switch (this. startpoint)

{

Case "0 ":

Return true;

Break;

Default:

String [] ST = startpoint. Split ('-');

If (dtnow. hour = int. parse (ST [0]) & (dtnow. minute = int. parse (ST [1]) & (dtnow. second <30 ))

{

Return true;

}

Break;

}

}

Return false;

}

Register your Windows service program.

Use installutil iasserver.exe (this file is under C:/winnt/Microsoft. NET/framework/v1.1.4322 by default)

Installutil iasserver.exe

Installutil iasserver.exe/U (unmount)

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.