Namespace for timerEvent
What will be the namespace for TimerEvent ?开发者_如何学编程
You seem to have copied some code out of this ebook (see page 183 on onwards). Basically, TimerEvent is a delegate that the author has created for their ClockTimer class.
TimerEvent
is not part of the .Net framework. You can create your own TimerEvent delegate as follows:
public event TimerEvent Timer;
public delegate void TimerEvent(object sender, EventArgs e);
System.Threading.Timer
Provides a mechanism for executing a method at specified intervals
MSDN Article
From Article
using System;
using System.Threading;
class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10);
// Create an inferred delegate that invokes methods for the timer.
TimerCallback tcb = statusChecker.CheckStatus;
// Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter.
Console.WriteLine("{0} Creating timer.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
// When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500);
Console.WriteLine("\nChanging period.\n");
// When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
}
class StatusChecker
{
private int invokeCount;
private int maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if(invokeCount == maxCount)
{
// Reset the counter and signal Main.
invokeCount = 0;
autoEvent.Set();
}
}
}
If you're looking for the .NET Timer class and the events generated by it, you can use the System.Timers namespace.
There is also a Timer class in System.Windows.Forms but this is single-threaded and less accurate than the above.
EDIT: As mentioned by @GenericTypeTea, it's not an existing part of the .NET framework so you'll need to either create a new delegate or use an existing event instead such as Timer.Tick.
精彩评论