zoukankan      html  css  js  c++  java
  • windows服务使用

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    namespace WinService
    {
        public partial class WinService : ServiceBase
        {
            private System.Timers.Timer theTimer = new System.Timers.Timer();//定时器
            private double timespan;//服务执行的时间间隔
            public WinService()
            {
                InitializeComponent();
                this.theTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.theTimer_Elapsed);
            }
            /// <summary>
            /// 服务启动时执行的函数
            /// </summary>
            /// <param name="args"></param>
            protected override void OnStart(string[] args)
            {
                // TODO: 在此处添加代码以启动服务。
                try
                {
                    theTimer.Start();
                    timespan =
    Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["timespan"]);
                    theTimer.Interval = timespan * 60 * 1000;//转换为毫秒
                }
                catch (Exception ex)
                {
                      EventLog.WriteEntry("WinService服务启动","错误:"+ex.Message);
                }
               
            }
            /// <summary>
            /// 定时触发事件函数
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void theTimer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
            {
                try
                {
                    BLog();
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry("WinService服务", ex.Message + DateTime.Now.ToString());
                }
            }
    /// <summary>
            /// 逻辑处理函数
            /// </summary>
            public void BLog()
            {
                DateTime nowTime = DateTime.Now;
                EventLog.WriteEntry("WinService服务", nowTime.ToString());
            }
            /// <summary>
            /// 服务停止时执行的函数
            /// </summary>
            protected override void OnStop()
            {
                // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
                EventLog.WriteEntry("WinService服务停止",  DateTime.Now.ToString());
            }
        }
    }
  • 相关阅读:
    linux c 正则表达式
    【编译原理】1. 宏观结构
    编译原理
    知我所未知,方行我所未至
    接口返回值中的状态值设置
    Quotes in shell(bash, csh)
    Blog 081018
    58同城职位分类数据 json
    ckeditor小记
    三大WEB服务器对比分析(apache ,lighttpd,nginx)
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2628963.html
Copyright © 2011-2022 走看看