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. 11.2.0.4单实例DRCP(Database Resident Connection Pooling)简单测试

    DRCP配置及测试 一. DRCP介绍 数据库提供会话进程在数据库中使用资源的方式: 1)Dedicated Server,一个会话在数据库中对应一个专有进程,一对一服务(资源数据库占用过多,一般使用 ...

  2. flask中的request和response

    1.request 1.request的常用成员 1.scheme : 获取请求方案(协议) 2.method : 获取本期请求的请求方式(重点) 3.args : 获取使用get请求方式提交的数据 ...

  3. jQ常用选择器

    #id:    $('#div'); //查找id='div'; .class: $('.myclass'); //查找class='myclass';​ element:用于搜索的元素,指向DOM节 ...

  4. poj 2253——Frogger

    这个题一开始不知道咋做,但是大致有点意思.后来还是借鉴了题解发现可以用dijkstra,不太理解.但是在最后自己推的时候突然理解了. dijkstra应该也算是动态规划.我们用dis[i]数组作为青蛙 ...

  5. 2018.10.25 CCSP马拉松摸铜归来

    24号体测跑50+1000米. 50米抢跑被罚重跑???然后老年人就只能吊着一口仙气跑第二次50米.然后跑1000米,然后再到宿舍收拾行李赶往地铁站,然后再冲到火车站...(卒) 宾馆,三人挤入二人房 ...

  6. mvc core2.1 Identity.EntityFramework Core ROle和用户绑定查看 (八)完成

    添加角色属性查看 Views ->Shared->_Layout.cshtml <div class="navbar-collapse collapse"> ...

  7. redis 部署相关

    ★ 启动: 配置好环境变量后:打开一个cmd窗口,执行redis-server.exe就可以启动redis了. https://blog.csdn.net/weixin_42423819/articl ...

  8. LG4781 【模板】拉格朗日插值

    题意 题目描述 由小学知识可知,$n$个点$(x_i,y_i)$可以唯一地确定一个多项式 现在,给定$n$个点,请你确定这个多项式,并将$k$代入求值 求出的值对$998244353$取模 输入输出格 ...

  9. architecture and business process modelling

    bpmn 架构相关的文章: 转自:https://www.heflo.com/definitions/architecture-business-process-modeling/ BPMN Mode ...

  10. httpd

    http://httpd.apache.org/docs/2.2/logs.html httpd.conf文件 Configuration and logfile names: If the file ...