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(); }
}

}
}

 

posted @ 2010-07-05 21:59  熊哥  阅读(629)  评论(0编辑  收藏  举报