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());
            }
        }
    }
  • 相关阅读:
    RT-Thread代码启动过程与$Sub$ $main、$Super$ $main
    软件开源许可证
    git回退到历史版本以及再滚回去
    GMT、UTC、UNIX时间戳、时区
    sprintf的使用
    C# Json 和对象的相互转换
    获取指定年份/月份的周六周天 + 标记指定日期(加粗)
    Winform 窗体实现圆角展示
    VS2012统计代码量
    C# Winform 中使用FTP实现软件自动更新功能
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2628963.html
Copyright © 2011-2022 走看看