利用TimerCallback实现timer传递参数
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace WindowsApplication1
{
class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker();
TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus);
Timer t = new Timer(timerDelegate, autoEvent, 0, 1000);
//Timer t = new Timer(timerDelegate, "时间: ", 0, 1000);
autoEvent.WaitOne(-1,false);
}
}
class StatusChecker
{
int i = 0;
public StatusChecker()
{
}
public void CheckStatus(Object str)
{
i++;
Console.WriteLine( DateTime.Now.ToString());
if (i == 10)
{
((AutoResetEvent)str).Set();
}
}
}
}
//http://msdn2.microsoft.com/zh-cn/library/system.threading.timercallback(VS.80).aspx#Mtps_DropDownFilterText
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace WindowsApplication1
{
class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker();
TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus);
Timer t = new Timer(timerDelegate, autoEvent, 0, 1000);
//Timer t = new Timer(timerDelegate, "时间: ", 0, 1000);
autoEvent.WaitOne(-1,false);
}
}
class StatusChecker
{
int i = 0;
public StatusChecker()
{
}
public void CheckStatus(Object str)
{
i++;
Console.WriteLine( DateTime.Now.ToString());
if (i == 10)
{
((AutoResetEvent)str).Set();
}
}
}
}
//http://msdn2.microsoft.com/zh-cn/library/system.threading.timercallback(VS.80).aspx#Mtps_DropDownFilterText