Windows Service Command Reprint

Source: Internet
Author: User

Oncustomcommand executes when the service Control Manager (SCM) passes a custom command to the service. Specifies actions to take when a command with the specified parameter value occurs.

The only values for a custom command so can define in your application or use in Oncustomcommand is those between 1 256.
integers below correspond to system-reserved values.

Create one Windows Service & Implement Below Code in Service:

namespaceMywindowsservice
{
PublicPartialclassService1:ServiceBase
{
PublicenumSimpleservicecustomcommands{Command1 = Command2, =129, Command3 = 130};

PublicService1 ()
{
InitializeComponent ();
}

        protected  void  onstart (string [] args)
        {
        }

        protected  void  onstop ()
        {
       }

        protected  void  oncustomcommand (int  command)
        {
             base . Oncustomcommand (command);

             switch (command)
                 case (int ) simpleservicecustomcommands . COMMAND1:
                 //command1 implementation
                 break ;

                 case (int ) simpleservicecustomcommands . COMMAND2:
                  break ;

Case (int) simpleservicecustomcommands. Command3:
//COMMAND3 Implementation
                     ;
                 default :
                      break ;
 
            }       
        }
   }}

Call Windows Service customcommands from User application:

  • Create Service Controller Object

    ServiceController Controller = new ServiceController("Mywindowsservice");
  • start the Windows service

    controller.refresh (); Gets the current status of Service
        if   ( Controller.status == servicecontrollerstatus . Stopped)
         {
     controller.start ( );
        }
     
  • Call Customcommands Using Controller Object

    if (Controller.status = = ServiceControllerStatus. Running)
    {
    Controller.executecommand (128);
    }

Windows Service Command Reprint

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.