Microsoft Windows Services

Source: Internet
Author: User
Tags net command

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. These services can be started automatically when the computer starts, can be paused and restarted, and does not display any user interface. This 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 a default computer account that is a logged-on user.

Development

0. Create a service 1. Install 2. Start 3. Stop 4. Uninstall 5. Monitoring Services

To create a service:

1. Creating 1 services in C # is simple, as long as you inherit ServiceBase, overriding the OnStart and OnStop methods. (The fastest is the right-click New Item)

The OnStart method is executed when the service is started.

The OnStop method is executed when the service is stopped.

  

2. Regular task processing is usually done with System.Timers.Timer.

Installation:

Mode One (manual):

InstallUtil:

1. Open Visual Studio. NET command prompt
2. Change the path to the Bin\Debug folder location where your project is located (if you are compiling in release mode in the Bin\release folder)
3. Execute the command "InstallUtil.exe MyWindowsService.exe" to register the service so that it establishes an appropriate registration entry.

Way Two (code):

A.managedinstallerclass class:

var FilePath = process.getcurrentprocess (). Mainmodule.filename (Take the current process file path)

var cmds = new []{filepath};

var cmds = new []{"\\u", FilePath};

Managedinstallerclass.installhelper (CMDS);

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

var cmd = "Create myserver binpath= \" "+ Program Absolute path name +" \ "displayname= Service name start= Auto"

Process.Start ("SC", CMD);

Start:

Way One (code):

A.process.start ("SC", "Start Service name");

Way Two (manual):

A. Command prompt: Net start service name

B. Run "Services.msc", locate the service name, start the service

Stop it:

Way One (code):

A.process.start ("SC", "Stop Service Name");

Way Two (manual):

A. Command prompt: net stop service name

B. Run "Services.msc", locate the service name, stop the service

Unloading:

Mode One (manual):

1. Open Visual Studio. NET command prompt
2. Change the path to the Bin\Debug folder location where your project is located (if you are compiling in release mode in the Bin\release folder)
3. Execute the command "installutil.exe/u MyWindowsService.exe" to register the service so that it establishes an appropriate registration entry.

Way Two (code):

Process.Start ("SC", "Delete Service name");

Monitoring:

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

This class can get the status of the service, the properties, the service can also start, stop the operation

Microsoft Windows Services

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.