First, create a window service
1. Create a new project--Select Windows services. The default build file includes Program.cs,service1.cs
2, add the following code in Service1.cs:
System.Timers.Timer timer1; Timer
Public Service1 ()
{
InitializeComponent ();
}
protected override void OnStart (string[] args)//service Start execution
{
Timer1 = new System.Timers.Timer ();
Timer1. Interval = 3000; Set Timer event interval execution time
Timer1. Elapsed + = new System.Timers.ElapsedEventHandler (timer1_elapsed);
Timer1. Enabled = true;
}
protected override void OnStop ()//Service Stop execution
{
this.timer1.Enabled = false;
}
private void Timer1_elapsed (object sender, System.Timers.ElapsedEventArgs e)
{
Execute SQL statements or other operations
}
Second, add the Window service installation program
1, open Service1.cs "Design" page, right click, select "Add Installer", will appear serviceInstaller1 and serviceProcessInstaller1 two components
2, set the account property of ServiceProcessInstaller1 to "LocalSystem", ServiceInstaller1 's StartType attribute is set to "Automatic", The ServiceName property sets the name of the service, which is then displayed in the Administrative tools--services
3, ProjectInstaller.cs file, after the installation of services generally also need to manually start (even if the above StartType property is set to "Automatic"), you can add the following code ProjectInstaller.cs to implement the installation of automatic start
Public ProjectInstaller ()
{
InitializeComponent ();
this.committed + = new Installeventhandler (projectinstaller_committed);
}
private void Projectinstaller_committed (object sender, Installeventargs e)
{
parameter is the name of the service
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController ("service name");
Controller. Start ();
}
Third, install, uninstall window service
1. Input cmd (command line), enter CD c:\windows\microsoft.net\framework\v4.0.30319,2.0 for CD C:\WINDOWS\Microsoft.NET\Framework\ v2.0.50727
2, Installation Services (project generated EXE file path)
InstallUtil "E:\WindowsService1\bin\Debug\WindowsService1.exe"
3. Uninstall Service
Installutil/u "E:\WindowsService1\bin\Debug\WindowsService1.exe"
Iv. Viewing window Services
Control Panel--Administration tools--services, which can be started manually, stop the service
V. Commissioning Window Services
1. Viewing through the Event Viewer
2, debugging directly in the Program (menu-and-debug-attach process--service name is the project name, not the name of the ServiceName property customization, so it is recommended that the custom name and the project name are consistent and that the "Show all users ' processes" is also checked.) To see the service name)--Attach
3. Interrupt point debugging in the program, and the service must be started when debugging services (Administration Tools-Services)
Text to from: http://blog.csdn.net/armyfai/article/details/8056976
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;
Using System.Data.SqlClient;
Using System.Threading;
Namespace Innpoint
{
public partial class Service:servicebase
{
Public Service ()
{
InitializeComponent ();
}
protected override void OnStart (string[] args)
{
EventLog.WriteEntry ("My service Startup");//Description of the source in the application event in the System Event Viewer
WRITESTR ("service start");//Custom text log
System.Timers.Timer t = new System.Timers.Timer ();
T.interval = 1000;
t.elapsed + = new System.Timers.ElapsedEventHandler (chksrv);//The event is executed at time of arrival;
T.autoreset = true;//Whether the setting is executed once (false) or always executed (true);
t.enabled = true;//Whether the System.Timers.Timer.Elapsed event is performed;
}
<summary>
Timed check, and execution method
</summary>
<param name= "source" ></param>
<param name= "E" ></param>
public void Chksrv (object source, System.Timers.ElapsedEventArgs e)
{
int inthour = E.signaltime.hour;
int intminute = E.signaltime.minute;
int intsecond = E.signaltime.second;
if (Inthour = = && Intminute = = && Intsecond = = 00)///Timing setting to determine time-sharing seconds
{
Try
{
System.Timers.Timer TT = (System.Timers.Timer) source;
Tt. Enabled = false;
Setinnpoint ();
Tt. Enabled = true;
}
catch (Exception err)
{
Writestr (Err. Message);
}
}
}
My way.
public void Setinnpoint ()
{
Try
{
WRITESTR ("service Run");
Here, execute your stuff.
Thread.Sleep (10000);
}
catch (Exception err)
{
Writestr (Err. Message);
}
}
Executes the specified expression after the specified time
///
Elapsed time (in milliseconds) between events
The expression to execute
public static void SetTimeout (double interval, action action)
{
System.Timers.Timer Timer = new System.Timers.Timer (interval);
Timer. Elapsed + = Delegate (object sender, System.Timers.ElapsedEventArgs e)
{
Timer. Enabled = false;
Action ();
};
Timer. Enabled = true;
}
public void Writestr (string readme)
{
debug==================================================
StreamWriter dout = new StreamWriter (@ "C: \" + System.DateTime.Now.ToString ("Yyymmddhhmmss") + ". txt");
StreamWriter dout = new StreamWriter (@ "C: \" + "Wserv_innpointlog.txt", true);
Dout. Write ("\ r \ n Event:" + Readme + "\ r \ n operation time:" + System.DateTime.Now.ToString ("Yyy-mm-dd HH:mm:ss"));
debug==================================================
Dout. Close ();
}
protected override void OnStop ()
{
WRITESTR ("service Stop");
EventLog.WriteEntry ("My service is stopped");
}
}
}
3. Right-click on the Service1.cs design page to add the installer
4.projectinstaller.cs the design page
The ServiceInstaller1 property is set in:
Description (Description of system service)
DisplayName (name displayed in system service)
ServiceName (source name in the application event in the System Event Viewer)
ServiceProcessInstaller1 property setting: Account drop-down set to LocalSystem
5. The. NET Framework installation directory can be found in InstallUtil.exe, which can be copied out and placed in your service generated EXE together
6. Installation Service Setup.bat batch file contents: InstallUtil Service1.exe, after installation, you can find the service name you set in the system services and then you can start the service
7. Uninstall Service Uninstall.bat batch file contents: Installutil-u Service1.exe
C # Creates a Windows service and executes it regularly