// 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