C # create a Windows Service (Windows Services) and restart the Service regularly

Source: Internet
Author: User

// Server Restart service, Author: Liu yongfa www.yongfa365.com

Using System;

Using System. Collections. Generic;

Using System. ComponentModel;

Using System. Data;

Using System. Diagnostics;

Using System. ServiceProcess;

Using System. IO;

Using System. Text;

Using System. Timers;

Namespace CBDCN_reboot

{

Public partial class Service1: ServiceBase

{

Public Service1 ()

{

InitializeComponent ();

}

Protected override void OnStart (string [] args)

{

// TODO: Add code here to start the service.

Double sleeptime = ValidatorDate1 (System. dateTime. now. toString ("yyy-MM-dd HH: mm: ss"), System. dateTime. now. toString ("yyy-MM-dd") + "07:00:00 ");

If (sleeptime <0) sleeptime + = 24x60x60*1000;

Writestr ("START", sleeptime );

System. Timers. Timer t = new System. Timers. Timer (sleeptime); // instantiate the Timer class and set the interval to 10000 milliseconds;

T. Elapsed + = new System. Timers. ElapsedEventHandler (rebootsystem); // 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;

Writestr ("end", sleeptime );

}

Public void rebootsystem (object source, System. Timers. ElapsedEventArgs e)

{

System. Diagnostics. Process. Start ("shutdown", "/r/f/t 0 ");

}

Public static double ValidatorDate1 (string strDateA, string strDateB)

{

String strFrom = strDateB;

String strTo = strDateA;

TimeSpan ts = Convert. ToDateTime (strFrom)-Convert. ToDateTime (strTo );

Double count = ts. totalsecondds * 1000;

Return count;

}

Public void writestr (string readme, double sleeptime)

{

// Debug ============================================ ==================

StreamWriter dout = new StreamWriter (@ "c:" + System. DateTime. Now. ToString ("yyyMMddHHmmss") + readme + ". txt ");

Dout. write ("SleepTime:" + Convert. toString (sleeptime) + "operation time:" + System. dateTime. now. toString ("yyy-MM-dd HH: mm: ss "));

// Debug ============================================ ==================

Dout. Close ();

}

Protected override void OnStop ()

{

}

}

}

Online Introduction

Use C # To create Windows Services)

Windows Services are called NT Services in earlier versions of Visual Studio, and new names are enabled in VS.net. Using Visual C # To create a Windows service is not difficult. This article will guide you step by step to create a Windows service and use it. When the service starts and stops, it writes some text information to a text file.

Step 1: create a service framework

To create a new Windows Service, you can select the Windows Service option from the Visual C # project, give the project a new file name, and click OK.

You can see that the wizard adds the WebService1.cs class to the project file:

The meaning of each attribute is:

Autolog

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.