C#.NET TaskServiceBase
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.ServiceProcess;
using System.Timers;
namespace Pub.Class {
public abstract class TaskServiceBase : ServiceBase {
private Timer timer = new Timer();
public TaskServiceBase(TimeSpan interval) {
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = interval.TotalMilliseconds;
timer.AutoReset = false;
timer.Enabled = false;
}
protected override void OnStart(string[] args) { timer.Start(); }
protected override void OnStop() { timer.Stop(); }
protected abstract void RunTask();
private void timer_Elapsed(object sender, ElapsedEventArgs e) {
timer.Stop();
try { RunTask(); } finally { timer.Start(); }
}
}
}
开源:
https://github.com/hcxiong 欢迎收藏:)