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服务命令 转载的更多相关文章

  1. cmd命令行和bat批处理操作windows服务(转载)

    一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32srv ...

  2. 注册tomcat为windows服务(转载)

    第一部分 应用场景 需要服务器上Tomcat不显示启动窗口 需要服务器上Tomcat开机自启动 ... 第二部分 配置过程 一.修改配置文件 1 {Tomcat_HOME}/bin/service.b ...

  3. 删除windows服务命令

    打开命令框:输入sc delete 服务名 例如删除elasticsearch-service-x64服务 sc delete elasticsearch-service-x64

  4. 玩转Windows服务系列——命令行管理Windows服务

    说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令,命令行都是非常方便以及强大的工具. 接下来就看一下如何通过cmd命 ...

  5. 玩转Windows服务系列——命令行管理Windows服务

    原文:玩转Windows服务系列——命令行管理Windows服务 说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令, ...

  6. [转]玩转Windows服务系列——命令行管理Windows服务

    本文转自:http://www.cnblogs.com/hbccdf/p/managewindowsservicewithcmd.html 说到Windows服务的管理就不得不说通过命令行的方式管理W ...

  7. ASP.NET Core Web程序托管到Windows 服务

    前言 在 .NET Core 3.1和WorkerServices构建Windows服务 我们也看到了,如何将workerservices构建成服务,那么本篇文章我们再来看看如何将web应用程序托管到 ...

  8. SC命令---安装、开启、配置、关闭 cmd命令行和bat批处理操作windows服务

      一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32s ...

  9. windows 运行打开服务命令

    转载自:http://www.2cto.com/os/201209/157464.html windows运行打开服务命令 Java代码  1. gpedit.msc-----组策略  2. sndr ...

随机推荐

  1. ubuntu sublime text key

    使用方法 打开 Sublime Text 3 的 “Help”–“Enter Licence”,然后根据版本选择输入下面的注册码. 注册码 所有这些注册码都经过测试(2016-05-12),适用于所有 ...

  2. golang 六宫格、九宫格头像生成

    图片示例就不传了,在原WordPress上. //Merge6Grid 6宫格 //rule NO1:至少3张图 最多6张图 // NO2:第一张大小 60*60 其他大小 28*28 间隔4px 合 ...

  3. git 应用

    git - 简易指南 助你开始使用 git 的简易指南,木有高深内容,;). 安装 下载 git OSX 版 下载 git Windows 版 下载 git Linux 版 创建新仓库 创建新文件夹, ...

  4. django 一个关于分组查询的问题分析

    ret=Emp.objects.values('dep').annotate(avg_salary=Avg('salary')) print(ret) # ---*******单表分组查询ORM总结: ...

  5. 《DSP using MATLAB》Problem 5.22

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% O ...

  6. 《DSP using MATLAB》Problem 5.12

    1.从别的地方找的证明过程: 2.代码 function x2 = circfold(x1, N) %% Circular folding using DFT %% ----------------- ...

  7. day25scala

    PS:1.scala是开发spark平台的一种语言.2.如果开发spark的话,用scala开发是非常好的,Python的话一般,用java的话就是效果不好. -------------------- ...

  8. day4 java消息中间件服务

    PS: 讲个故事,老王要给他的两个女儿讲故事,他要一个一个讲很费劲,后来他使用了微信公众号,让订阅微信公众号的人关注就减轻了负担. PS: 传统的如果一个用户进行登录,会调用分多的服务,如果没有消息中 ...

  9. MySQL Export--导出数据

    =========================================================== SELECT INTO OUTFILE语法格式: SELECT [columns ...

  10. Unity 资源的优化管理 学习