I. What is the window service?
When you click Start, Run the services. msc command. The following window is displayed. It displays the services that come with the system or third-party software installed in the current operating system. The service status includes the name, description, startup status, and Startup type. You can also manage related services in this form. Be careful when performing operations to prevent system errors or software faults.
2. Create a Windows Service
1) create a Windows Service Project
2) Rename the service
Rename service1 as your service name. Here we name it servicetest.
2. Create a service Installer
1) Add the installer
Then we can see that projectinstaller. CS and two installed components are automatically created for us.
2) modify the name of the installation service
Right-click serviceinsraller1, select properties, and change the value of servicename to servicetest.
3) modify the installation permission
Right-click serviceprocessinsraller1, select properties, and change the account value to LocalSystem.
3. Write Service Code
1) Open servicetest code
Right-click servicetest and choose view code.
2) Write Service Logic
Add the following code: using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. LINQ;
Using system. serviceprocess;
Using system. text;
Namespace windowsservicetest
{
Public partial class servicetest: servicebase
{
Public servicetest ()
{
Initializecomponent ();
}
Protected override void onstart (string [] ARGs)
{
Using (system. Io. streamwriter Sw = new system. Io. streamwriter ("C: \ log.txt", true ))
{
Sw. writeline (datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS") + "start .");
}
}
Protected override void onstop ()
{
Using (system. Io. streamwriter Sw = new system. Io. streamwriter ("C: \ log.txt", true ))
{
Sw. writeline (datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS") + "stop .");
}
}
}
}
Here, our logic is very simple. Write a log when starting the service, and then write a log when it is disabled.
Iv. Install and uninstall Windows Services
Method 1: cmd command
Click Start and run the CMD command. In the displayed cmd window, install or uninstall the service.
1. Install the service: Sr start is the startup service.
The basic commands are as follows:
% SystemRoot % \ microsoft.net \ framework \ v2.0.50727 \ installutil.exe E: \ demo \ windowsservice \ windowsservicetest \ bin \ debug \ windowsservicetest.exe
SC start "servicetest"
Method 2:BAT file method:
Method 1 is used to install and uninstall the service commands in servicetestinstaller. BAT and servicetestuninstaller. bat. In addition, to write scripts for the login user, you can develop a tool named createservicesetup.exe to generate the installation and uninstallation script BAT file for the window service.
The servicetestinstaller. Bat code is as follows:
% SystemRoot % \ microsoft.net \ framework \ v4.0.30319 \ installutil.exe E: \ demo \ windowsservice \ windowsservicetest \ bin \ debug \ windowsservicetest.exe SC start "servicetest" Echo servicetest service has been installed! Echo. & pause
1. Install the service
Step 1: Double-click servicetestinstaller. BAT to install the service. The interface is as follows:
Step 2: for example, if the result shows that the service is successfully installed, go to the Service Management page and check whether the service appears. As shown in. Start -- run, enter services. MSC
2. Uninstall the service
The servicetestinstaller. Bat code is as follows:
% SystemRoot % \ microsoft.net \ framework \ v4.0.30319 \ installutil.exe/u e: \ demo \ windowsservice \ windowsservicetest \ bin \ debug \ windowsservicetest.exe SC stop "servicetest" Echo servicetest service has been uninstalled! Echo. & pause
Run servicetestinstaller. bat, for example:
4. How to debug the window service
First install the service, and then the rest will be the same as debugging for common programs.
Note that the installation path should be the service program EXE file under bin \ debug \ where the program is located.
V. Use Value of the window service
1. Create scheduled tasks.
2. Create a server monitoring program.
3. Public Application access interfaces.
4. other purposes.
Vi. Window service use drills