using System; using System.Threading;class TimerExample { static void Main() { AutoResetEvent autoEvent = new AutoResetEvent(false); StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback(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(timerDelegate, 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 { int invokeCount, 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(); } } }
第一个代码示例以一秒为增量单位跟踪每天的时间。它在窗体中使用了 Button、Label 和 Timer 组件。将 Interval 属性设置为 1000(等于一秒钟)。在 Tick 事件中,将标签的标题设置为当前时间。当单击按钮时,Enabled 属性将设置为 false,以使计时器停止更新标签的标题。下面的代码示例要求您拥有一个窗体,该窗体具有一个名为 Button1 的 Button 控件、一个名为 Timer1 的 Timer 控件和一个名为 Label1 的 Label 控件。private void InitializeTimer() { //' Run this procedure in an appropriate event. // Set to 1 second. Timer1.Interval = 1000; // Enable timer. Timer1.Enabled = true; Button1.Text = "Stop"; }private void Timer1_Tick(object Sender, EventArgs e) { // Set the caption to the current time. Label1.Text = DateTime.Now.ToString(); }private void Button1_Click() { if ( Button1.Text == "Stop" ) { Button1.Text = "Start"; Timer1.Enabled = false; } else { Button1.Text = "Stop"; Timer1.Enabled = true; } }
第二个代码示例每隔 600 毫秒运行一次过程,直到循环完成时为止。下面的代码示例要求您拥有一个窗体,该窗体具有一个名为 Button1 的 Button 控件、一个名为 Timer1 的 Timer 控件和一个名为 Label1 的 Label 控件。 // This variable will be the loop counter. private int counter;private void InitializeTimer() { // Run this procedure in an appropriate event. counter = 0; timer1.Interval = 600; timer1.Enabled = true; // Hook up timer's tick event handler. this.timer1.Tick += new System.EventHandler(this.timer1_Tick); }private void timer1_Tick(object sender, System.EventArgs e) { if (counter >= 10) { // Exit loop code. timer1.Enabled = false; counter = 0; } else { // Run your procedure here. // Increment counter. counter = counter + 1; label1.Text = "Procedures Run: " + counter.ToString(); } }
using System.Threading;class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate =
new TimerCallback(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(timerDelegate, 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
{
int invokeCount, 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();
}
}
}
{
//' Run this procedure in an appropriate event.
// Set to 1 second.
Timer1.Interval = 1000;
// Enable timer.
Timer1.Enabled = true;
Button1.Text = "Stop";
}private void Timer1_Tick(object Sender, EventArgs e)
{
// Set the caption to the current time.
Label1.Text = DateTime.Now.ToString();
}private void Button1_Click()
{
if ( Button1.Text == "Stop" )
{
Button1.Text = "Start";
Timer1.Enabled = false;
}
else
{
Button1.Text = "Stop";
Timer1.Enabled = true;
}
}
// This variable will be the loop counter.
private int counter;private void InitializeTimer()
{
// Run this procedure in an appropriate event.
counter = 0;
timer1.Interval = 600;
timer1.Enabled = true;
// Hook up timer's tick event handler.
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}private void timer1_Tick(object sender, System.EventArgs e)
{
if (counter >= 10)
{
// Exit loop code.
timer1.Enabled = false;
counter = 0;
}
else
{
// Run your procedure here.
// Increment counter.
counter = counter + 1;
label1.Text = "Procedures Run: " + counter.ToString();
}
}