C# 定时任务
Code
using System;
using System.Threading;
public class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker();
TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus);
Console.WriteLine("{0} Creating timer.\n",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
Timer stateTimer = new Timer(timerDelegate, null, 0, 1000);
autoEvent.WaitOne();
}
}
class StatusChecker
{
public StatusChecker()
{
}
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
string hour = DateTime.Now.Hour.ToString();
string minute = DateTime.Now.Minute.ToString();
string second = DateTime.Now.Second.ToString();
if (int.Parse(hour) < 10)
{
hour = "0" + hour;
}
if (int.Parse(minute) < 10)
{
minute = "0" + minute;
}
if (int.Parse(second) < 10)
{
second = "0" + second;
}
Console.WriteLine("Now time is {0}:{1}:{2}", hour, minute,second);
string now = hour + minute + second;
if (now == "163900")
{
Console.WriteLine("Do Your Method!");
}
}
}
using System;
using System.Threading;
public class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker();
TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus);
Console.WriteLine("{0} Creating timer.\n",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
Timer stateTimer = new Timer(timerDelegate, null, 0, 1000);
autoEvent.WaitOne();
}
}
class StatusChecker
{
public StatusChecker()
{
}
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
string hour = DateTime.Now.Hour.ToString();
string minute = DateTime.Now.Minute.ToString();
string second = DateTime.Now.Second.ToString();
if (int.Parse(hour) < 10)
{
hour = "0" + hour;
}
if (int.Parse(minute) < 10)
{
minute = "0" + minute;
}
if (int.Parse(second) < 10)
{
second = "0" + second;
}
Console.WriteLine("Now time is {0}:{1}:{2}", hour, minute,second);
string now = hour + minute + second;
if (now == "163900")
{
Console.WriteLine("Do Your Method!");
}
}
}