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),
4.0:CD C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
2.0: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
Services.msc
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
The process name attached here should be: WindowsService1.exe instead of WindowsService1.vshost.exe. WindowsService1.exe default does not appear, you must tick "show all user's processes" "Show processes in all sessions"
3. Interrupt point debugging in the program, and the service must be started when debugging services (Administration Tools-Services)
C # Creates a Windows service. Service function: Timed operation database