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());
            }
        }
    }
  • 相关阅读:
    CobaltStrike上线Linux主机(CrossC2)
    Active-Directory活动目录备忘录
    CVE-2020-5902 F5 BIG-IP 远程代码执行漏洞复现
    SSTI-服务端模板注入漏洞
    powershell代码混淆绕过
    绕过PowerShell执行策略方法
    "dpkg: 处理归档 /var/cache/apt/archives/libjs-jquery_3.5.1+dfsg-4_all.deb (--unpack)时出错"的解决方法
    firda安装和使用
    内网渗透-跨域攻击
    Web-Security-Learning
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2628963.html
Copyright © 2011-2022 走看看