C # Writing Windows service (Windows Services Program)

Source: Internet
Author: User

    • Introduction to Windows service:

A Windows service program is an executable application that can perform certain functions under the Windows operating system. Although the Windows service program is executable, it does not start running as a generic executable file by double-clicking it, it must have a specific startup mode. These boot methods include both auto-start and manual start -up. For Windows service programs that start automatically, they start executing before Windows starts or restarts before the user logs on. As long as you register the appropriate Windows service program with the Service Control Manager and set its startup category to start automatically. For a Windows service program that starts manually, you can start it either by using the NET start command of the command line tool or by starting the appropriate Windows service program by using the services under Administrative Tools in Control Panel.

Similarly, a Windows service program cannot be terminated as a generic application. Because Windows service programs generally do not have a user interface, you also need to stop it either through the command-line tool or the tools in the diagram below, or when the system shuts down, causing the Windows service program to stop automatically. Because the Windows service program does not have a user interface, the API functions based on the user interface do not make much sense to them. In order to enable a Windows service program to work properly and effectively in a system environment, programmers must implement a series of methods to complete their service functions. Windows Services programs are widely used, and typical Windows Services programs include hardware control, application monitoring, system-level applications, diagnostics, reporting, Web, and file system services.

The namespaces associated with Windows service programs involve the following two:system.serviceprocess and system.diagnostics.

    • Steps to create a Windows service in C #:

1. Create a Windows Service project

Select the Windows Services (Windows Service) option from the Visual C # project, give the project a new file name, and then click OK.

2. Implementing function functions in the service

The OnStart function executes when the service is started, and the OnStop function executes when the service is stopped. Here, when starting and stopping the service, write some text message to a text file, the code is as follows:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.diagnostics;using system.io;using system.linq;using system.serviceprocess;using System.Text;using        System.threading.tasks;namespace myservice{public partial class Service1:servicebase {public Service1 ()        {InitializeComponent (); } protected override void OnStart (string[] args) {FileStream fs = new FileStream (@ "D:\xx.txt", F            Ilemode.openorcreate, FileAccess.Write);            StreamWriter SW = new StreamWriter (FS); Sw.            Basestream.seek (0, Seekorigin.end); Sw.            WriteLine ("Windowsservice:service Started" + DateTime.Now.ToString () + "\ n"); Sw.            Flush (); Sw.            Close (); Fs.        Close (); }//protected override void OnContinue ()//{//base.        OnContinue (); }//protected override void OnPause ()//{//base. OnPause (); FatheR class method inherit//}//protected override void OnShutdown ()//{//base.        OnShutdown (); } protected override void OnStop () {FileStream fs = new FileStream (@ "D:\xx.txt", FileMode.Open            Orcreate, FileAccess.Write);            StreamWriter SW = new StreamWriter (FS); Sw.            Basestream.seek (0, Seekorigin.end); Sw.            WriteLine ("Windowsservice:service Stopped" + DateTime.Now.ToString () + "\ n"); Sw.            Flush (); Sw.            Close (); Fs.        Close (); }    }}

4. Right-click Back to design Window- Add installer -build serviceInstaller1 and serviceProcessInstaller1 two components
Rewrite the ServiceInstaller1 attribute servicename to your service program name and set the startup mode to Automatic
Rewrite the ServiceProcessInstaller1 attribute account to LocalSystem


5. Compile the link to the service program

Build the project by selecting Build from the Build menu.

6. Installation Services

Install the service program with the. NET Framework Tools InstallUtil.

Run InstallUtil.exe from the command line with the output of the project as a parameter. Enter the following code on the command line:
InstallUtil Yourproject.exe

Hint:a Windows service must first be installed using Installutil.exe and then started with the Serviceexplorer, Windows S Ervices administrative tool or the NET START command.

7. Uninstall the service

Run InstallUtil.exe from the command line with the output of the project as a parameter.

Installutil/u Yourproject.exe

As of the service program running results:

    • Add:

1.Service Startup properties:

After the Manual service is installed, it must be started manually.

Automatic the service will start automatically every time the computer restarts.

The Disabled service could not be started.

2. New service project with the meanings of each property (Design view, right-click Property):

Autolog whether to automatically write to the system's log files

Canhandlepowerevent receive power events during service

Whether the CanPauseAndContinue service accepts requests that are paused or continue to run

Whether the CanShutdown service receives a notification when the computer on which it is running is closed so that the OnShutdown procedure can be called

Whether the CanStop service accepts a request to stop running

ServiceName Service Name

3. You can also set the properties or startup mode of the corresponding service in the system Service Manager.

Services, services and applications, Computer Management ...

C # Writing Windows service (Windows Services Program)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.