码迷,mamicode.com
首页 > Windows程序 > 详细

.Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

时间:2018-09-07 18:37:06      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:color   生成   hive   als   nts   service   ram   ima   执行   

 

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.NET\Framework\v2.0.50727\installutil.exe 路径Release  Service.exe
Net Start 服务名
sc config 服务名 start= auto

Uninstall.bat

%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\installutil.exe /u  路径Release  Service.exe

 

.Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

标签:color   生成   hive   als   nts   service   ram   ima   执行   

原文地址:https://www.cnblogs.com/xiaoruilin/p/9606445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!