码迷,mamicode.com
首页 > Windows程序 > 详细

windows服务命令 转载

时间:2016-01-30 18:21:35      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

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 that you can define in your application or use in OnCustomCommand are those between 128 and 256.
Integers below 128 correspond to system-reserved values.

Create One Windows Service & Implement Below Code in Service :

namespace MyWindowsService
{
    public partial class Service1 : ServiceBase
    {
        public enum SimpleServiceCustomCommands { Command1 = 128, Command2 =129, Command3 = 130};
 
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }

        protected override void OnCustomCommand(int command)
        {
            base.OnCustomCommand(command);

            switch(command)
            {
                case(int)SimpleServiceCustomCommands.Command1:
                //Command1 Implementation
                break;

                case(int)SimpleServiceCustomCommands.Command2:
                //Command2 Implementation
                 break;

                case(int)SimpleServiceCustomCommands.Command3:
                //Command3 Implementation
                    break;
                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服务命令 转载

标签:

原文地址:http://www.cnblogs.com/baozhu/p/5171207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!