在Service服务文件夹下新建Windows服务 - TestService
更改服务名称:
一、Timer方式:
using System; using System.Text; using System.Data; using System.Diagnostics; using System.Configuration; using System.ComponentModel; using System.ServiceProcess; using System.Collections.Generic; using Test.UserCenter.Common; namespace Test.UserCenter.BillRemindService { public partial class BillRemindService : ServiceBase { private System.Timers.Timer timer = null; private Logger logger = Logger.GetInstance(); public BillRemindService() { AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e) { this.logger.WriteErrLog("BillRemindService", "BillRemindService", "出现未处理异常:" + e.ExceptionObject, new Exception()); }; InitializeComponent(); } protected override void OnStart(string[] args) { logger.WriteInfoLog("BillRemindService", "OnStop", "提醒服务开始"); timer = new System.Timers.Timer(); timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
//10分钟执行一次 timer.Interval = 1000 * Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalSec"].ToString()); timer.Enabled = true; timer.Start(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs args) { try { BillRemindProcess process = new BillRemindProcess(); process.RevMQ();//执行方法 } catch (Exception ex) { this.logger.WriteErrLog("BillRemindService", "timer_Elapsed", "", ex); ex = null; } } protected override void OnStop() { logger.WriteInfoLog("BillRemindService", "OnStop", "提醒服务停止"); } } }
二、线程方式
using System; using System.Data; using System.Text; using System.Threading; using System.Configuration; using System.Collections.Generic; using System.ComponentModel; using System.ServiceProcess; using Test.UserCenter.Common; namespace Test.UserCenter.MsgAlertRemindService { public partial class MsgAlertRemindService : ServiceBase { #region 变量定义 /// <summary> /// 处理MQ线程 /// </summary> private Thread processThread; /// <summary> /// 日志记录 /// </summary> private Logger logger = Logger.GetInstance(); /// <summary> /// MsgAlertRemindProcess /// </summary> private MsgAlertRemindProcess process = new MsgAlertRemindProcess(); /// <summary> /// TimerIntervalSec /// </summary> public static int TimerIntervalSec = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalSec"].ToString()); #endregion #region MsgAlertRemindService /// <summary> /// MsgAlertRemindService /// </summary> public MsgAlertRemindService() //构造函数 对象初始化 { AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs ex) { logger.WriteErrLog("MsgAlertRemindService", "MsgAlertRemindService", "出现未处理异常," + ex.ExceptionObject, new Exception()); ex = null; }; InitializeComponent(); this.processThread = new Thread(new ThreadStart(ProcessMsgAlertRemind)); //创建线程对象 this.processThread.IsBackground = true; //后台线程 解决前台卡死的问题 } #endregion #region OnStart /// <summary> /// OnStart /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { if (!this.processThread.IsAlive) //一开始线程没有开启 判断没开启 开始执行线程 { this.processThread.Start(); } } #endregion #region OnStop /// <summary> /// OnStop /// </summary> protected override void OnStop() { } #endregion #region ProcessMsgAlertRemind /// <summary> /// ProcessMsgAlertRemind /// </summary> private void ProcessMsgAlertRemind() { while (true) { this.process.Process(); //调用执行方法 Thread.Sleep(1000 * TimerIntervalSec); //线程休眠十分钟 } } #endregion } }
app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <!--timer计时器间隔时间,单位(秒)--> <add key="TimerIntervalSec" value="600"/> 十分钟 </appSettings> </configuration>