C # compile Windows Services,

Source: Internet
Author: User

C # compile Windows Services,
1. Introduction to Windows Services:

A Windows Service Program is an executable application that can complete specific functions in a Windows operating system. Although a Windows service program is executable, it does not run as a normal executable file by double-clicking it. It must have a specific startup method. These methods include automatic start and Manual start. For Windows service programs that are automatically started, they are executed before the user logs on after Windows is started or restarted. You only need to register the corresponding Windows Service Program in Service Control Manager and set the startup category to Automatic startup. For a Windows service program that is manually started, you can use the net start command of the command line tool to START it, or start the corresponding Windows service program through the service item under the management tool in the control panel.

Similarly, a Windows service program cannot be terminated as a normal application. Because Windows service programs generally do not have a user interface, you need to stop them using the command line tool or the tool shown in the figure below, or make the Windows Service Program stop automatically when the system is disabled. Because Windows service programs do not have a user interface, the user interface-based API functions have little significance for them. To enable a Windows service program to work normally and effectively in a system environment, programmers must implement a series of methods to complete its service functions. Windows service programs have a wide range of applications. Typical Windows service programs include hardware control, application monitoring, system-level applications, diagnostics, reporting, Web and file system services.

The namespaces related to Windows service programs involve System. ServiceProcess and System. Diagnostics.

2. Create a Windows Service:

Create a Windows Service Project on VS: Right-click to add a service by program

Select serviceProcessInstaller1> modify attributes:

Select serviceInstaller1 and modify the attributes:

1. Service Startup attributes:

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

The service is automatically started every time the computer restarts.

The Disabled service cannot be started.

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

Computer Management-> services and applications-> services->...

 

Public partial class Service1: ServiceBase {public Service1 () {InitializeComponent ();} protected override void OnStart (string [] args) {Thread. sleep (10000); // rest is to attach to the process for breakpoint debugging // start service business logic} protected override void OnStop (){}}

After compilation, install the service.

III. 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

The directory where InstallUtil.exe is located the directory where Windows Services are located // note that there is a space in the middle

4. uninstall service: run the cmd console application as an administrator and 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

InstallUtil.exe's directory-u Windows Service's directory // use the-u command to uninstall

Related Article

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.