Create and Control Windows services--not a Slap Shot, but almost as fast[rank: advanced]

Source: Internet
Author: User
Services|window| Advanced Not a Slap Shot, but almost as Fast
Creating the service using. NET is fairly straightforward. Use the. NET Framework ' s system.serviceprocess namespace and four classes within It:servicebase, ServiceInstaller, Se Rviceprocessinstaller, and ServiceController. These classes provide most of the functionality your need to create a service (for the Figure 1).


Figure 1 | Everything you Need in one Namespace. Click here.


The ServiceBase class defines methods a derived class can override so the Service control Manager can control a service-in Cluding OnStart (), OnStop (), OnPause (), and OnContinue (). Furthermore, a service can override the Oncustomcommand () function to carry out specific operations to it sent Cally by a external service controller (more in this later). Implementing these functions in your derived class gives your necessary of a service:

protected override void OnStart (string[) args)
{
}
protected override void OnStop ()
{
}
protected override void OnPause ()
{
}
protected override void OnContinue ()
{
}
Allow your service to begin execution by defining a entry point into your executable, as and would with a regular ble. Within the Main () function, create a instance of your class and call the Run () method defined by the ServiceBase base CLA Ss. If you are want your executable to support multiple services, create a array of ServiceBase objects with each element CORRESP Onding to one of your defined services:

static void Main ()
{
System.serviceprocess.servicebase[] myservices;

MyServices = new system.serviceprocess.servicebase[] {new Service1, New Service2 ()};

System.ServiceProcess.ServiceBase.Run (myservices);
}
Note this Run () does not start your service. In the "other words", you won ' t receive a OnStart () call from the Run () method itself. As I mentioned before, the service control Manager controls the service by calling the necessary controlling.


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.