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;
    using System.Threading;
    using log4net;
    using System.Configuration;
    namespace Beyondbit.Comac.DocumentsUserSynch
    {
        partial class UsersSynch : ServiceBase
        {
            private System.Timers.Timer theTimer = new System.Timers.Timer();//定时器
            private double timespan;//服务执行的时间间隔
            public UsersSynch()
            {
                InitializeComponent();
                this.theTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.theTimer_Elapsed);
    
            }
            private ILog _Log = log4net.LogManager.GetLogger("Beyondbit.Comac.DocumentSyn");
            protected ILog Log
            {
                get
                {
                    return _Log;
                }
            }
          //  private Thread _WorkThread;
            protected override void OnStart(string[] args)
            {
                // TODO: 在此处添加代码以启动服务。
                //_WorkThread = new Thread(new ThreadStart(WorkThreadHandle));
                //_WorkThread.Start();
                try
                {
                    Log.Debug("-===========OnStart方法!-============-");
                    theTimer.Start();
                    timespan = Convert.ToDouble(ConfigurationManager.AppSettings["jianGeHour"]);
                    theTimer.Interval = timespan * 60 * 60 * 1000; //转换为毫秒
                    theTimer.AutoReset = true;
                    theTimer.Enabled = true;
                }
                catch (Exception ex)
                {
                    DateTime now = DateTime.Now;
                    Log.Error("-===========OnStart(...)方法出错:" + ex.Message); 
                }
            }
                  private void theTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                try
                {
                    DateTime now = DateTime.Now;
                    Log.Debug("-===========UsersSynch类,执行theTimer_Elapsed()方法!-============-时间:" + now.ToString());
                    string[] shijian = (ConfigurationManager.AppSettings["dingshi"]).Split(':');
                    int hour = int.Parse(shijian[0]);//
                 
                    if (now.Hour == hour)
                    {
                        Log.Debug("-===========准备执行processUserSynch.PrecessSynchData()方法!-============-时间:" + now.ToString());
                        for (int i = 0; i < 2; i++)
                        {
                            ProcessUserSynch processUserSynch = new ProcessUserSynch();
                            processUserSynch.PrecessSynchData();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("-===========theTimer_Elapsed(...)方法出错:"+ex.Message);
                }
            }
            protected override void OnStop()
            {
                // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
                Log.Debug("-===========OnStop(),服务停止!===========-");
            }
        }
    }

    作者:沐雪
    文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者和博客园共有,如需转载恳请注明。
    如果您觉得阅读这篇博客让你有所收获,请点击右下方【推荐】
    找一找教程网-随时随地学软件编程 http://www.zyiz.net/

  • 相关阅读:
    芯片测试
    【转】悬浮的对话框
    imagebutton 设置点击和按压效果
    imagebutton 设置了src属性的图片更换
    侧滑实现
    使用自定义的AlertDialog。
    [转]Dialog
    【转】webview的几个问题
    webview 播放H5视频问题 黑屏 只有声音没有画面
    【转】Android HTML5 Video视频标签自动播放与自动全屏问题解决
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2628978.html
Copyright © 2011-2022 走看看