Microsoft Windows Services (that is, previous NT services) enable you to create executable applications that can run for long periods of time in their own Windows sessions.
The service can be started automatically when the computer starts, can be paused and restarted, and does not display any user interface.
The service is ideal for use on a server, or at any time, in order not to affect other users working on the same computer that require long-running functionality.
You can also run a service in a different security context than a specific user account or default computer account for a logged-on user
First, create a service
1. VS Create a new Windows service project named Mywindowsservice
2, modify Service1 for Myservice,f7 Enter the code page (click Switch Code view), write the Operation logic code
3, OnStart for the execution of service events, the general use of threading methods to facilitate the execution of a single event
usingSystem;usingSystem.IO;usingsystem.serviceprocess;usingSystem.Threading.Tasks;namespacemywindowsservice{ Public Partial classMyservice:servicebase { PublicMyService () {InitializeComponent (); } //Service Open protected Override voidOnStart (string[] args) {Task.Factory.StartNew (Handle); } //Service shutdown protected Override voidOnStop () {}//code snippets that require regular execution Private voidHandle () { while(true) { Try { varPath = AppDomain.CurrentDomain.BaseDirectory +"Service.log"; varContext ="mywindowsservice:service stoped"+ DateTime.Now +"\ n"; Writelogs (path, context); } Catch(Exception) {Throw; } } } Public voidWritelogs (stringPathstringcontext) { varFS =NewFileStream (Path, FileMode.OpenOrCreate, FileAccess.Write); varSW =NewStreamWriter (FS); Sw. Basestream.seek (0, Seekorigin.end); Sw. WriteLine (context); Sw. Flush (); Sw. Close (); Fs. Close (); } }}
Service cannot be run directly, F5 will appear
Second, create the service installation program
1. Select MyService.cs, right--and view the designer; right---Add the installer in the designer
2. The ProjectInstaller.cs file is added to the project, and the view in the file automatically adds two components ServiceProcessInstaller1 and ServiceInstaller1
3, check the ServiceProcessInstaller1 component, view the properties, set account to LocalSystem, so as to ensure that no matter what user login can be performed.
4. Check the ServiceInstaller1 component to see the property settings as follows
ServiceName: Represents the name in the system service
Starttype:manual: Manual start, default stop, Automatic: Auto start
Description: Service Description
Third, installation, uninstall services
Installation Services need InstallUtil.exe, generally found in the computer directory C:\Windows\Microsoft.NET\Framework64\v4.0.30319
It is important to note that there are many similar versions under the C:\Windows\Microsoft.NET\Framework directory, which directories need to be viewed in the context of the project, for example, if the. NET framework2.0 is required to enter a CD C:\Windows\ microsoft.net\framework\v2.0.50727
1. Rebuild the Mywindowsservice Project
2, the generated bin directory to the directory to be installed (the service file is already in the attachment): For example: Copy to \ c (Service path installed according to the actual situation, uninstall)
3. Window +r Open cmd
CD C:\Windows\Microsoft.NET\Framework64\v4.0.30319
Installation Services: InstallUtil C:\bin\Debug\MyWindowsService.exe
Uninstall Service: installutil/u C:\bin\Debug\MyWindowsService.exe
4. My Computer--right-click Manage-Services and applications--service find our service
Handle () method write log is executed after service is started
Four, commissioning services
First make sure the service is installed successfully and is in the started
Vs--> Debug-Attach to process find our service name Mywindowsservice OK
Then the breakpoint is in the code.
If there is a problem with MyWindowsService.exe not replicating or process consuming
Open the Process manager and close the MyWindowsService.exe process.
C # Developing Windows Services