Microsoft Windows Service (1) C # create a Windows Service,

Source: Internet
Author: User
Tags net command

Microsoft Windows Service (1) C # create a Windows Service,

Microsoft Windows Services (formerly known as NT Services) enable you to create executable applications that can run for a long time in their own Windows sessions. These services can be automatically started when the computer is started. They can be paused and restarted without displaying any user interface. This kind of service is very suitable for use on the server, or any time, in order not to affect other users working on the same computer, it requires a long time to run the function. You can also run the service in a security context different from the logon user's specific user account or default computer account.

 

 

Development

0. Create a Service 1. Install 2. Start 3. Stop 4. Uninstall 5. Monitor the service

 

Create a service:

1. It is very easy to create a service in c #. You only need to inherit ServiceBase and override the OnStart and OnStop methods. (The fastest way is to right-click to create an item)

The OnStart method is executed when the service is started.

The OnStop method is executed when the service is stopped.

  

2. System. Timers. Timer is usually used for scheduled task processing.

 

Installation:

Method 1 (manual ):

InstallUtil:

1. Prompt for opening the Visual Studio. NET command
2. Change the path to the bin \ Debug folder of your project (if you compile in Release mode, in the bin \ Release folder)
3. Execute the command "installutil.exe MyWindowsService.exe" to register this service and create an appropriate registration item.

 

Method 2 (CODE ):

A. ManagedInstallerClass class:

Var filePath = Process. GetCurrentProcess (). MainModule. FileName (take the path of the current Process file)

Var cmds = new [] {filePath };

ManagedInstallerClass. InstallHelper (cmds );

 

B. (The SC command is a command line program used to communicate with the Service Control Manager and service)

Var cmd = "create myserver binpath = \" "+ absolute program path name +" \ "displayName = service name start = auto"

Process. Start ("SC", cmd );

 

Start:

Method 1 (CODE ):

A. Process. Start ("SC", "start service name ");

 

Method 2 (manual ):

A. Name of the net start service in the command prompt

B. Run "services. msc", find the service name, and start the service.

 

Stop:

Method 1 (CODE ):

A. Process. Start ("SC", "stop Service name ");

 

Method 2 (manual ):

A. net stop service name in the command prompt

B. Run "services. msc", find the service name, and stop the service.

 

Uninstall:

Method 1 (manual ):

1. Prompt for opening the Visual Studio. NET command
2. Change the path to the bin \ Debug folder of your project (if you compile in Release mode, in the bin \ Release folder)
3. Execute the command "installutil.exe/u MyWindowsService.exe" to register this service and create an appropriate registration item.

 

Method 2 (CODE ):

Process. Start ("SC", "delete service name ");

 

Monitoring:

Use ServiceController to obtain the service status or control the service.

This class can obtain the service status, attributes, and services.Start and StopOperation

 

 

Code download

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.