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
  • 相关阅读:
    Vector Dictionary HashMap
    崩溃java (打印当前类的地址)
    colspan、rowspan的使用
    基础操作
    安装vuex-devtools插件
    在使用Vuex插件进行状态管理的步骤
    运行cnpm run dev出现错误解决办法
    在使用vue开发时,文件路径的引用(给文件起别名)
    下载脚手架vue.cli出现的问题
    什么是闭包?
  • 原文地址:https://www.cnblogs.com/xiaoruilin/p/9606445.html
Copyright © 2011-2022 走看看