MCAD Learning notes Microsoft Windows services

Source: Internet
Author: User
Tags implement

Windows service is running in the background

It has no user interface and is better suited for tasks that do not interact with the user

It can run on win2000,winxp,winnt and other operating systems.

about how to write a simple Windows service application

We can refer to

Http://chs.gotdotnet.com/QuickStart/howto/default.aspx?url=/quickstart/howto/doc/SimpleService.aspx

I have made a small summary of my personal learning experience here:

Microsoft Windows Services uses classes in system.serviceprocess space to create, install, and control

which

Class System.ServiceProcess.ServiceBase to create a service

Class System.ServiceProcess.ServiceInstaller and System.ServiceProcess.ServiceProcessInstaller are used to install services

ServiceInstaller class extends servicebase to implement services. The class is called by the installation utility when the service application is installed

ServiceProcessInstaller installs an executable file that contains classes that extend the ServiceBase. This class is invoked by the installation utility (such as InstallUtil.exe) when the service application is installed

Let's first recognize some of the more important attributes and methods of ServiceBase:

Property ServiceName is the name of the service

Property AutoLog Indicates whether the start, stop, pause, and continue commands are reported in the event log.

When it is true, the system automatically reports the status in the Application event log

When the AutoLog is set to false, we can process our own logs in the event

Property CanStop Indicates whether the service can be stopped after it is started

If true, the service can stop and invoke the OnStop method

The same attributes CanShutdown and Canpauseandcontinue,canshutdown are similar in meaning

The virtual method OnStop occurs when the service receives the Stop command, noting that when Canstop=false does not call this method

Virtual method OnStart is when the service receives the "START" command

Virtual method OnPause occurs when the service receives a pause command (when the CanPauseAndContinue property is true)

Virtual method OnContinue is done when the service is "paused" (when the CanPauseAndContinue property is true)

Virtual method OnShutdown occurs when the computer system shuts down (when the CanShutdown property is true)

Virtual method Onpowerevent is performed when the computer's power state changes (when the Canhandlepowerevent property is true)

Virtual method Oncustomcommand is performed when a service is received from a custom command, and I have the implementation of the method in the code provided later.

All you need to do is rewrite the above method in your own code to implement your own business logic.

Click the Properties window for each service, you will find a "Add Installer" button, which is the Windows Service installation feature in VS2003

It actually generates an installation class that inherits the System.Configuration.Install.Installer

After compiling we get an EXE of the assembly

You can then use the. NET Framework SDK Installation utility to install the service to our computer

Install Command:

InstallUtil.exe Filename.exe

Anti-install command:

Installutil.exe/u Filename.exe

After the service is installed, we can control our service operation through the Service Control Manager in the Windows Control Panel, and of course we can write code to implement the control. At this time we have to use the class System.ServiceProcess.ServiceController, the use of this class is relatively simple. You can refer to the Help document specifically.

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.