windows服务的通常写法

简介: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.
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(),服务停止!===========-");
        }
    }
}
相关文章
|
9天前
|
安全 Linux iOS开发
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
29 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
|
6月前
|
NoSQL Redis Windows
windows服务器重装系统之后,Redis服务如何恢复?
windows服务器重装系统之后,Redis服务如何恢复?
98 6
|
2月前
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
189 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
|
4月前
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
263 4
|
5月前
|
边缘计算 安全 网络安全
|
5月前
|
开发框架 .NET API
Windows Forms应用程序中集成一个ASP.NET API服务
Windows Forms应用程序中集成一个ASP.NET API服务
132 9
|
5月前
|
应用服务中间件 Apache Windows
免安装版的Tomcat注册为windows服务
免安装版的Tomcat注册为windows服务
174 3
|
5月前
|
Java 关系型数据库 MySQL
java控制Windows进程,服务管理器项目
本文介绍了如何使用Java的`Runtime`和`Process`类来控制Windows进程,包括执行命令、读取进程输出和错误流以及等待进程完成,并提供了一个简单的服务管理器项目示例。
76 1
|
6月前
|
Java 应用服务中间件 Windows
windows服务器重装系统之后,Tomcat服务如何恢复?
windows服务器重装系统之后,Tomcat服务如何恢复?
106 10
|
6月前
|
消息中间件 Java Kafka
windows服务器重装系统之后,Kafka服务如何恢复?
windows服务器重装系统之后,Kafka服务如何恢复?
55 8