/// <summary>
/// 在指定的时间唤醒指定的程序
/// </summary>
public class Planner:IDisposable
{
private GOA.Interface.IPlanalbe _plan = null;
private string tag;
private DateTime settingTime;
private System.Timers.Timer timer = null;
/// <summary>
/// 唤醒器
/// </summary>
/// <param name="timeExpression">时间表达式,定义在什么时候唤醒</param>
/// <param name="plan">需要唤醒的程序</param>
public Planner(string timeExpression , GOA.Interface.IPlanalbe plan)
{
tag = timeExpression.Trim();
_plan = plan;
InitTimer();
}
private void InitTimer()
{
timer = new System.Timers.Timer(200);
SetNotifyTime(DateTime.Now);
timer.Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.AutoReset = true;
timer.Start();
}
private void SetNotifyTime(DateTime dt)
{
string[] t = tag.Split(':');
//System.Windows.Forms.MessageBox.Show(this.tag);
int hour = int.Parse(t[0]);
int min = int.Parse(t[1]);
settingTime = new DateTime(dt.Year,dt.Month,dt.Day,hour,min,0);
if(settingTime < DateTime.Now)
{
settingTime = settingTime.AddDays(1);
}
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if(settingTime.ToShortDateString() == DateTime.Now.ToShortDateString() && settingTime.ToShortTimeString() == DateTime.Now.ToShortTimeString())
{
try
{
Utils.SystemUtil.DebugLog("Planner::timer_Elapsed::Begin");
timer.AutoReset = false;
timer.Stop();
Utils.SystemUtil.DebugLog("Planner::timer_Elapsed::1");
_plan.Action();
Utils.SystemUtil.DebugLog("Planner::timer_Elapsed::2");
}
finally
{
SetNotifyTime(DateTime.Now.AddDays(1));
timer.AutoReset = true;
timer.Start();
}
Utils.SystemUtil.DebugLog("Planner::timer_Elapsed::End");
}
}
#region IDisposable 成员
public void Dispose()
{
Utils.SystemUtil.DebugLog("Planner::Dispose::Begin");
if(timer != null)
{
Utils.SystemUtil.DebugLog("Planner::Dispose::IN");
timer.Stop();
timer.Dispose();
}
Utils.SystemUtil.DebugLog("Planner::Dispose::End");
}
#endregion
}
using System;
using System.Threading;
using System.Timers;
public class WaitableTimerSample
{
public static void Main(String[] args)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimer);
aTimer.Interval = 1000;
aTimer.Enabled = true;
Console.WriteLine("按“q”退出该示例");
while(Console.Read()!='q');
}
public static void OnTimer(Object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
Imports System
Imports System.Threading
Imports System.Timers
Namespace Timer
Module Timer
Public Sub Main()
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer
AddHandler aTimer.Elapsed, AddressOf OnTimer
aTimer.Interval = 1000
aTimer.Enabled = True
Console.WriteLine("按""q""退出该示例")
While (Console.Read() <> 113)
End While
End Sub
Public Sub OnTimer(ByVal source As Object, ByVal e As ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Module
End Namespace
public partial class MyService : ServiceBase
{
private System.Timers.Timer timer1;
/// <remarks>
/// Required designer variable.
/// </remarks>
private System.ComponentModel.Container components = null;
public MyService()
{
// This call is required by the Windows.Forms
// Component Designer.
InitializeComponent();
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new MyService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Interval = 30000;
this.timer1.Elapsed +=
new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// MyService
//
this.ServiceName = "My Sample Service";
((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
this.timer1.Enabled = false;
}
private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
//
}
}