zoukankan      html  css  js  c++  java
  • 使用C#开发Windows服务程序

    Windows服务介绍

    Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序。这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面。这使服务非常适合在服务器上使用,或任何时候,为了不影响在同一台计算机上工作的其他用户,需要长时间运行功能时使用。还可以在不同于登录用户的特定用户帐户或默认计算机帐户的安全上下文中运行服务。本文就向大家介绍如何运用Visual C#来一步一步创建一个文件监视的Windows服务程序,然后介绍如何安装、测试和调试该Windows服务程序。

    创建Windows服务

    创建好项目之后 --- >> 双击 Service1.cs  ---- >>  出现一个设计界面   ---->> 右键界面  --- >> 弹出对话框选择添加安装程序

     重命名服务的名称

    点击服务上的属性,修改名称和属性内容。

    双击添加代码

    namespace IdleMonitorServer
    {
        partial class ProjectInstaller
        {
            /// <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.MonitorGameServer = new System.ServiceProcess.ServiceProcessInstaller();
                this.ServerStatus = new System.ServiceProcess.ServiceInstaller();
                // 
                // MonitorGameServer
                // 必须添加的代码段
                this.MonitorGameServer.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                this.MonitorGameServer.Password = null;
                this.MonitorGameServer.Username = null;
                // 
                // ServerStatus
                // 
                this.ServerStatus.Description = "用于监控服务器工作状态。";
                this.ServerStatus.DisplayName = "ServerStatus";
                this.ServerStatus.ServiceName = "ServerStatus";
                // 
                // ProjectInstaller
                // 
                this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                this.MonitorGameServer,
                this.ServerStatus});
    
            }
    
            #endregion
    
            private System.ServiceProcess.ServiceProcessInstaller MonitorGameServer;
            private System.ServiceProcess.ServiceInstaller ServerStatus;
        }
    }

      

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    
    
    namespace IdleMonitorServer
    {
        public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
    
            }
            Platform_MonitorIdleServerTask MonitorServerTask = new Platform_MonitorIdleServerTask();
            Platform_SendAlarmMail SendWarningMail = new Platform_SendAlarmMail();
            protected override void OnStart(string[] args)
            {
                
                MonitorServerTask.Start();
                
                SendWarningMail.Start();
            }
    
            protected override void OnStop()
            {
                MonitorServerTask.Stop();
    
                SendWarningMail.Stop();
            }
    
            protected override void OnPause()
            {
                base.OnPause();
            }
    
            protected override void OnContinue()
            {
                base.OnContinue();
            }
    
            protected override void OnShutdown()
            {
                base.OnShutdown();
            }
        }
    }

    安装卸载windows服务

    1、安装需要用,一般在C盘下会有"C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe"

    2、把InstallUtil.exe放到你编写好的服务程序/bin/Debug文件夹下。

    3、

    4、用命令读到你服务.exe文件夹下。

    5、运行 installutil.exe 

    6、安装服务命令: installutil  yourservices.exe

    7、卸载服务命令: installutil  /u  yourservices.exe

  • 相关阅读:
    一篇文章高效定位iframe
    URL与视图函数的映射
    include标签—引用文件路径
    UnitTest单元测试框架解析【实用篇】
    【案例演练】测试器与模板继承
    2招带你快速获取响应头与发送头
    dede网站如何更新地图sitemap.html
    申请微信小程序流程步骤
    phpstudy本地配置--dede织梦网
    stylus样式开发的使用----vue
  • 原文地址:https://www.cnblogs.com/annkiny/p/5093518.html
Copyright © 2011-2022 走看看