zoukankan      html  css  js  c++  java
  • [转]创建Windows服务 C#

    1.创建C#工程

      创建一个EXE工程,可以是WinForm或者命令行格式的。添加对System.ServiceProcess.dll和System.Configuration.Install.dll的引用。

    2. 创建服务类

      新增一个类,该类型继承System.ServiceProcess.ServiceBase类型,在构造函数中设置ServiceName 属性来指明该服务的名称。然后重载它的OnStart方法来响应启动服务的事件,重载OnStop方法来响应停止服务的事件,重载OnPause方法来响 应暂停服务的事件,重载OnContinue方法来响应恢复提供服务的事件。 

      在重载这些方法时必须要立即返回,其中不能出现长时间的操作,若处理时间过长则Windows服务管理器会觉得该Windows服务停止响应而报错。为此 我们可以使用一个线程来进行实际的工作,而OnStart方法创建线程,OnStop方法关闭线程,OnPause方法挂起线程,而OnContinue 方法来恢复运行线程。

    3.启动服务 

      在main函数中调用“System.ServiceProcess.ServiceBase.Run( 自定义服务类的实例 )”来运行服务。比如“System.ServiceProcess.ServiceBase.Run( new MyService() )”,这里的MyService就是继承自ServiceBase。

      注:以上步骤可以通过在VS2005中新建“Windows服务”项目,代码均由开发环境自动生成
    -------------------------------------------------------------------------------------------------------------

     4、创建服务的安装类

      新增一个类,该类型继承自System.Configuration.Install.Installer类型,该类型用于配合微软.NET框 架自带的安装命令行工具InstallUtil.exe的。我们为该类型附加 System.ComponentModel.RunInstallerAttribute特性,

    [RunInstaller(true)]
        
    public partial class ServiceInstaller :Installer
        {
            
            
    public ServiceInstaller()
            {
                InitializeComponent();
            }
        }

      并在它的构造函数中使用 System.ServiceProcess.ServiceInstaller对象和 System.ServiceProcess.ServiceProcessInstaller对象向系统提供该服务的安装信息。

    代码
     partial class ServiceInstaller
        {
            
    /// <summary>
            
    /// 必需的设计器变量。
            
    /// </summary>
            private System.ComponentModel.IContainer components = null;

            
    /// <summary> 
            
    /// 清理所有正在使用的资源。
            
    /// </summary>
            
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                
    if (disposing && (components != null))
                {
                    components.Dispose();
                }
                
    base.Dispose(disposing);
            }

            
    #region 组件设计器生成的代码

            
    /// <summary>
            
    /// 设计器支持所需的方法 - 不要
            
    /// 使用代码编辑器修改此方法的内容。
            
    /// </summary>
            private void InitializeComponent()
            {
                
    this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
                
    this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
                
    // 
                
    // serviceInstaller1
                
    // 
                this.serviceInstaller1.ServiceName = "ZhengShuChang";
                
    // 
                
    // serviceProcessInstaller1
                
    // 
                this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
                
    this.serviceProcessInstaller1.Password = null;
                
    this.serviceProcessInstaller1.Username = null;
                
    // 
                
    // ServiceInstaller
                
    // 
                this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                
    this.serviceInstaller1,
                
    this.serviceProcessInstaller1});

            }

            
    #endregion

            
    private System.ServiceProcess.ServiceInstaller serviceInstaller1;
            
    private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;

        }

      该步骤实际上是新建一个继承 System.Configuration.Install.Installer的类。然后通过设计界面 ,从Toolbox中向其添加ServiceProcessInstaller组件和ServiceInstaller组件。此时系统自动生成代码。然后须设置ServiceInstallerServiceName属性(要求和服务类的name一致)和ServiceProcessInstallerAccount属性

    5、安装\删除服务

      编译该工程文件,执行如下安装命令“”:

    @echo.
    @if exist "%windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" goto INSTALL2.0

    :INSTALL2.0
    @echo ◎ 开始安装服务2.0
    @%windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe    WindowsMonitorService.exe
    @echo.
    @echo √ 服务安装完成!
    @goto Ed

    :INSTALL1.0
    @echo ◎ 开始安装服务1.0
    %windir%\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe    WindowsMonitorService.exe
    @echo.
    @echo √ 服务安装完成!
    @goto Ed

     
    :ERR
    @echo.
    @echo × 程序运行期间出错,安装过程失败,请重新安装本软件!
    @echo off

    :Ed
    @echo.
    @echo 按任意键退出......
    @pause>nul
    @exit

      
      删除服务命令*.bat

    @echo.
    @if exist "%windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" goto INSTALL2.0

    :INSTALL2.0
    @echo ◎ 开始卸载服务...
    @%windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe    WindowsMonitorService.exe -u
    @del InstallUtil.InstallLog
    @del TestWindowsService.InstallLog
    @echo.
    @echo √ 服务卸载完成!
    @goto Ed

    :INSTALL1.0
    @echo ◎ 开始卸载服务...
    %windir%\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe    WindowsMonitorService.exe -u
    @del InstallUtil.InstallLog
    @del TestWindowsService.InstallLog
    @echo.
    @echo √ 服务卸载完成!
    @goto Ed

     
    :ERR
    @echo.
    @echo × 程序运行期间出错,卸载过程失败,请重新运行!
    @echo off

    :Ed
    @echo.
    @echo 按任意键退出......
    @pause>nul
    @exit


    5、服务客户端控制程序

      由于Windows服务是没有用户界面的,可以编写一个具有用户界面的程序来显示和控制 Windows服务提供的数据,并进行一些系统设置等操作。使用ServiceController类。通过ServiceController.GetServices()可以得到当前计算机的所有Windows服务。

    ServiceController[] services= ServiceController.GetServices(); 

      然后遍历services,通过serviceName找到需要的服务。从而使用ServiceController类的Start()、Stop()等方法与服务当前状态Status属性,即可对该Windows服务进行控制。

  • 相关阅读:
    Mybatis—动态sql拼接问题
    小白用Mac
    JSP总结(jsp/EL表达式/核心标签)
    Spring定时任务配置
    通过简单示例来理解什么是机器学习
    在jupyter notebook中同时安装python2和python3
    Python读取和处理文件后缀为".sqlite"的数据文件
    Python:Anaconda安装虚拟环境到指定路径
    TIOBE:全球编程语言最新排名(Kotlin排名进入前50名)
    Python:一篇文章掌握Numpy的基本用法
  • 原文地址:https://www.cnblogs.com/76674718/p/1642992.html
Copyright © 2011-2022 走看看