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. Python学习笔记第十一周

    目录: 1.RabbitMQ   2.Redis  内容: 1.RabbitMQ 实现简单的队列通信 send端 import pika credentials = pika.PlainCredent ...

  2. IDEA激活

    https://blog.csdn.net/qq_34273222/article/details/78810799 目测这个服务器可用:http://idea.iteblog.com/key.php

  3. 2017-2018-2 20165228 实验四《Android程序设计》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:苏祚堃 学号:20165228 指导教师:娄嘉鹏 实验日期:2018年5月14日 实验时间:13:45 - 3:25 实验序号:实验四 ...

  4. Python中的分支结构和循环结构

    一.分支结构 语法: if  条件  :     .... else :    .... 例子: num = int(input("输入一个整数:")) if num<=10 ...

  5. Python之路,第十篇:Python入门与基础10

    python3 函数 函数(function) 什么是函数: 函数是可以重复执行的代码块,可以重复使用: 作用: 定义用户级的函数:实现了一个代码块的封装: 语法: def   函数名(参数列表): ...

  6. easyui的DataGrid的单元格添加ProgressBar进度条

    网上的搜到的好多不能用,官方easy-ui使用进度条 <div id="p" class="easyui-progressbar" ></di ...

  7. 牛客G-指纹锁【一题三解】

    链接:https://www.nowcoder.com/acm/contest/136/G来源:牛客网 题目描述     HA实验有一套非常严密的安全保障体系,在HA实验基地的大门,有一个指纹锁.   ...

  8. FZU软工第十一次作业-软件产品案例分析

    目录 前言: 第一部分.调研,评测: 1.1.初次感觉: 1.2.企业号bug: 1.3.你觉得为什么这个产品组的人没有发现这些bug: 1.4.假设你们团队需要开发这套系统,需要注意哪些方面: 2. ...

  9. 迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)

    本题代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...

  10. Java基础一(开发环境、注释、关键字、标识符、数据)

    1.Java开发环境搭建2.HelloWorld案例3.注释.关键字.标识符4.数据(数据类型.常量) ###01java语言概述 * A: java语言概述 * a: Java是sun公司开发的一门 ...