zoukankan      html  css  js  c++  java
  • windows服务命令 转载

    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); 
           }
       

  • 相关阅读:
    基于xmpp openfire smack开发之Android客户端开发[3]
    简单工厂,抽象工厂,工厂模式三者的对照
    【独具慧眼 推荐有礼】找出您心中的技术大牛 活动開始啦!
    ZOJ 3201
    具体解释站点沙盒期的原因表现与解决的方法
    工作,究竟意味着什么
    MVC之查询demo
    学习开淘宝网店
    快排的两种写法
    QTableWidget具体解释(样式、右键菜单、表头塌陷、多选等) (非代理)
  • 原文地址:https://www.cnblogs.com/baozhu/p/5171207.html
Copyright © 2011-2022 走看看