using System;
using System.Timers;
namespace e1
{
class Class1
{
static DateTime time = new DateTime(2003,5,14,2,3,2);
static void Main(string[] args)
{
Timer timer = new Timer(86400000);
timer.Elapsed += new ElapsedEventHandler(TimeAdd);
timer.Start();
if(time.Date==DateTime.Now)
Console.WriteLine("Success");//加入要做的事件 else
Console.WriteLine("Fail");
} static void TimeAdd(object source,ElapsedEventArgs e)
{
time.AddDays(1); //天数加一
}
}
}
不过,你得让这个程序每分每秒运行着!
using System.Timers;
namespace e1
{
class Class1
{
static DateTime time = new DateTime(2003,5,14,2,3,2);
static void Main(string[] args)
{
Timer timer = new Timer(86400000);
timer.Elapsed += new ElapsedEventHandler(TimeAdd);
timer.Start();
if(time.Date==DateTime.Now)
Console.WriteLine("Success");//加入要做的事件 else
Console.WriteLine("Fail");
} static void TimeAdd(object source,ElapsedEventArgs e)
{
time.AddDays(1); //天数加一
}
}
}
不过,你得让这个程序每分每秒运行着!
using System.Timers;namespace timServices {
class Class1 {
static void Main(string[] args) {
System.Timers.Timer mytimer = new System.Timers.Timer();
// 设置Timer 的间隔时间
// 60 * 60 * 1000 就是3600 秒 * 1000 毫秒,也就是每小时一次
mytimer.Interval = 60 * 60 * 1000; mytimer.Elapsed += new ElapsedEventHandler(mytimer_Poc);
mytimer.Start(); //这里是其它事件或代码
} // 设置为每天 5 点执行一次
private static int myHour = 5; static void mytimer_Poc(object o,ElapsedEventArgs e) {
if (DateTime.Now.Hour == myHour) {
//事件处理 }
}
}
}