zoukankan      html  css  js  c++  java
  • .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

    Windows Service(服务)  是运行在后台的进程

    1、VS建立 Windows 服务(.NET Framework)

    2、添加Timer

    双击Service1.cs可以拖控件(System.Windows.Forms.Timer)这儿注意命名空间哦,

    双击 trmer1 生成事件,修改事件方法如下:

    App.config:

    <appSettings>
        <add key="TimerExecTime" value="0001-01-01 10:07:00"/>
      </appSettings>

    Service1.cs

    private void timer1_Tick(object sender, EventArgs e)
            {
                _logger.Info("timer1_Tick");
                DateTime execTime = DateTime.Parse(ConfigurationManager.AppSettings["TimerExecTime"]);
                _logger.Info("timer1_Tick" + execTime.Year);
                if (execTime.Year == 1)
                {
                    _logger.Info("yea"+ execTime.Hour + "Minute"+ execTime.Minute);
                    DateTime curTime = DateTime.Now;
                    if (execTime.Hour == curTime.Hour && execTime.Minute == curTime.Minute)
                    {
                        //每天几小时几分执行代码
                    }
                }
            }

    进入Service1.Designer.cs  修改如下:

            #region 组件设计器生成的代码
    
            /// <summary> 
            /// 设计器支持所需的方法 - 不要修改
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                //this.timer1 = new System.Timers.Timer(this.components);
                this.timer1 = new System.Timers.Timer();
                // 
                // timer1
                // 
                this.timer1.Interval = 30000;
                //this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    
                this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
                this.timer1.AutoReset = true;//设置是执行一次(false)还是一直执行(true); 
                this.timer1.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; 
                // 
                // Service1
                // 
                this.ServiceName = "Service1";
    
            }
    
            #endregion
    
            public System.Timers.Timer timer1;

    3、调试方法引用:

    https://www.cnblogs.com/xiebin1986/archive/2011/12/15/2288893.html

    4、安装与卸载

    在Service1.cs 右键=》添加安装程序 ,生成 ProjectInstaller.cs

    serviceInstaller1 设置如下图:

    serviceProcessInstaller1设置如下图:

    生成时

    Install.bat

    %SystemRoot%Microsoft.NETFrameworkv2.0.50727installutil.exe 路径Release  Service.exe
    Net Start 服务名
    sc config 服务名 start= auto

    Uninstall.bat

    %SystemRoot%Microsoft.NETFrameworkv2.0.50727installutil.exe /u  路径Release  Service.exe
  • 相关阅读:
    ffmpeg rtmp推流 视频转码
    java日志发展史 log4j slf4j log4j2 jul jcl 日志和各种桥接包的关系
    nginx stream 流转发,可以转发rtmp、mysql访问流,转发rtmp、jdbc请求
    java web http 转https 通过nginx代理访问
    linux 服务器磁盘挂载
    novnc 通过websockify代理 配置多点访问
    linux 文件服务 minio 安装部署配置
    AOP实现原理,手写aop
    java 泛型
    JAVA反射getGenericSuperclass()用法
  • 原文地址:https://www.cnblogs.com/xiaoruilin/p/9606445.html
Copyright © 2011-2022 走看看