C # Writing Windows services

Source: Internet
Author: User

I. Introduction to Windows Services:

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.

Two. Create a Windows service:

Create a new Windows Service project on vs: Right-click to add a service according to the program

Select Serviceprocessinstaller1> to modify the properties inside:

Select ServiceInstaller1 to modify the properties inside:

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.

After the installation is complete, you can also set the properties or startup mode of the service in System Service Manager.

Services, services and applications, Computer Management ...

 Publicpartial class Service1 : ServiceBase {public Service1 () {Initializecompon ENT (); } protected override  void OnStart (string[] args) {Thread.Sleep (10000); // rest is to attach to the process for breakpoint debugging //< Span style= "color: #008000;" > start service business logic  protected void OnStop () {}}     

The installation of the service is done after the compilation is completed.

Three. Install the service run the CMD console application as an administrator, enter: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe C: \users\yangyihe\ Desktop\c3. Apns\c3. apns.pushserver\bin\debug\ service. exe

InstallUtil.exe directory where the Windows service is located//note there are spaces in the middle

Four. Uninstall service: Run the CMD console application as Administrator, enter: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe-u C:\Users\ Yangyihe\desktop\c3. Apns\c3. apns.pushserver\bin\debug\ service. exe

directory where the InstallUtil.exe is located//the directory where the Windows service is located//uninstall using the-u command

C # Writing Windows services

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.