Windows service applicationsProgramIt is a program that runs on the background of the operating system for a long time. It is especially suitable for the server environment. It has no user interface and does not produce any visual output, any user output is written back to the Windows event log. When the computer starts, the service starts to run automatically. They do not run only when users log on to the computer.
You can choose "start"> "Control Panel"> "Management Tools"> "services" to view the services in the existing system, for example:
Create window service
Create a window service project myservice, as shown in figure
Drag a timer object from the component table of the toolbox to the view designer.
Set the timer attribute, enable to true, and interval to 3000 milliseconds.
Double-click timer event to add event
Background code
1 Public Partial Class Service1: servicebase 2 { 3 Public Service1 () 4 { 5 Initializecomponent (); 6 } 7 8 Protected Override Void Onstart (String [] ARGs) 9 { 10 // Todo: addCodeTo start the service. 11 } 12 13 Protected Override Void Onstop () 14 { 15 // Todo: Add code here to stop the service. 16 } 17 18 Private Void Timersponelapsed ( Object Sender, system. Timers. elapsedeventargs E) 19 { 20 21 } 22 }
1 Public Partial Class Service1: servicebase 2 { 3 Public Service1 () 4 { 5 Initializecomponent (); 6 } 7 Protected Override Void Onstart ( String [] ARGs) 8 { 9 // Todo: Add code here to start the service. 10 String State = datetime. Now. tostring ( " Yyyy-mm-dd hh: mm: SS " ) + " Start " ; 11 Writelog (State ); 12 } 13 Protected Override Void Onstop () 14 { 15 // Todo: Add code here to stop the service. 16 String State = datetime. Now. tostring ( " Yyyy-mm-dd hh: mm: SS " ) + " Stop " ; 17 Writelog (State ); 18 } 19 Private Void Timersponelapsed ( Object Sender, system. Timers. elapsedeventargs E) 20 { 21 Writelog (datetime. Now. tostring ( " Yyyy-mm-dd hh: mm: SS " )); 22 } 23 Public Void Writelog ( String Str) 24 { 25 Using (Streamwriter Sw = file. appendtext ( @" C: \ service.txt " )) 26 { 27 Sw. writeline (STR ); 28 Sw. Flush (); 29 } 30 } 31 }
Here, onstart and onstop are the event operation methods after the server is started and stopped, respectively, and writelog is the operation method;
Switch the service program service1.cs to view mode, right-click the design view, and select the "add installer" option. A project Installer. CS is automatically added to the project, as shown in figure
Set serviceinstaller1 component properties,
Servicename = Name of the myservicelog Installation server;
Starttype = Automatic startup
Design the attribute account = LocalSystem of serviceprocessinstaller1;
Run the compilation. A simple Windows service has been developed.
Note: If the file in the Code is written as service.txt, the file is stored inC: \ windows \ system32Folder.
Install window service
Installation command: installutil.exe myservicelog.exe
Installutil path: C: \ WINDOWS \ Microsoft. NET \ framework \. Net version
Copy installutil.exe from c: \ windows \ Microsoft. NET \ framework \ version to the bin/debug or bin/release folder and run the command directly in the command line window.
Installutil.exe myservicelog.exe,Register this service in the system to create an appropriate registration item, such:
Then, start the myservicelog service in the window service list.
Uninstall window service
Command: installutil.exe myservicelog.exe/u
If you modify the service but the path does not change, you do not need to register the service again. Stop the service and overwrite the original file with the new file. If the path changes, uninstall the service and then reinstall it.
Window service application architecture
. NET Framework provides more support for Windows Services, in the namespace system. serviceprocess.
Includes the following classes:
Base classes of all Windows Services in servicebase
An instance of the servicecontroller class represents a specific Windows Service
Servicecontrollerpermission is used to control the permission to use servicecontroller.
Serviceinstaller is used to install Windows Services.
Serviceprocessinstaller is used to install Windows Services. Different from the preceding class, this class can represent a Windows service process that can be executed.
Servicebase class
Servicebase method:
Method |
Description |
Run () |
Run a Windows Service |
Oncontinue () |
Continue executing the service |
Oncustomcommand () |
Issue custom commands to Windows Services |
Onpause () |
Pause a running Windows Service |
Onpowerevent () |
When the computer's power status changes, it is called. |
Onshutdown () |
When the system disables an entry, use onstart () to start a Windows service. |
Onstop () |
End a Windows Service |
Servicebase attributes:
Attribute |
Description |
Autolog |
Indicates whether the event log is written by the start, end, pause, and continue commands. |
Canhandlepowerevent |
Indicates whether the Service supports power events. |
Canpauseandcontinue |
Indicates whether the Service supports the pause and continue functions. |
Canshutdown |
Indicates whether the Service supports disabling the function. |
Canstop |
Indicates whether the Service supports the structure function. |
EventLog |
Application time log |
Servicename |
Service name |
Servicecontroller class
Service control class, used to control various specific behaviors of Windows Services. It can control both local Windows Services and remote Windows Services.
Servicecontroler method:
Method |
Description |
Getdivices () |
Get the device drive service on a computer |
Getservices () |
Obtains a non-device drive service on a computing instance. |
Close () |
Used to disconnect a service and release the resources occupied by the service controller. |
Continue () |
Continue suspended services |
Excutecommand () |
Execute a custom command for the service |
Pause () |
Suspend Service |
Refresh () |
Update all attributes |
Start () |
Start the service |
Stop () |
Stop Service |
Waitforstatus () |
Wait for the service to arrive at the specified status |