1. What is Windows service?
Windows service applications are applications that require long-term running. They are especially suitable for server environments. It does not have a user interface and does not produce any visual output. Any user message is written into the Windows event log. When the computer starts, the service automatically starts running. They do not run only when users log on. They can run in any user environment including the system. Through the Service Control Manager, Windows Services are controllable and can be terminated, paused, and started as needed.
This article demonstrates how to create a basic step of Windows service and use installutil.exe to publish the service so that it runs on the machine. The main function is to write down logs when the service starts and ends, and record the program running logs at a certain time.
2. Create a Windows Service
1. Create a project-Windows Service
2. write code for service1.cs
Todo... code is here...
Note: The timer here is effece. Timers. Timer, instead of system. Windows. Forms. Timer. For details about the differences, see another article.
Iii. configuration before Windows Service Installation
Windows Services are different from common Windows applications. It is not enough to simply execute an EXE program. the installutil.exetool of net framework is used to complete the process and upload the program to the Windows service. This makes it easier for installutil.exe or other installation programs to know how your service is configured.
3. Add a Windows Service Installer
1. Switch the service program to the design view.
2. Right-click the design view and select "add installer"
3. Switch to the design view of the newly added projectinstaller.
4. Set the attributes of the serviceinstaller1 component:
1) servicename = my first Windows Service
2) starttype = automatic
5. Set the attributes of the serviceprocessinstaller1 component
1) account = LocalSystem
6. Generate a solution
To end, this service has been generated, and you can use installutil.exe to install the Windows service.
Install Windows Services Using installutil.exe
1. Windows + R open cmd command prompt line
2. Switch the directory to the bin \ DEBUG directory of the project folder,
3. Run installutil.exe mywindowsservice.exe to register this service.
Note: directly running installutil.exemay fail, and it prompts that 'installutil.exe 'is not an internal or external command or a runable program or batch file.
The system does not recognize this command, but you can find this tool in the C:/Windows/Microsoft. NET/framework/v2.0.50727 directory.
4. Right-click "my computer"-the new Windows service is displayed in the service list.