本文整理匯總了C#中System.Timers.Timer.Elapsed事件的典型用法代碼示例。如果您正苦於以下問題:C# Timer.Elapsed事件的具體用法?C# Timer.Elapsed怎麽用?C# Timer.Elapsed使用的例子?那麽, 這裏精選的事件代碼示例或許可以為您提供幫助。您也可以進一步了解該事件所在類System.Timers.Timer
的用法示例。
在下文中一共展示了Timer.Elapsed事件的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Timers;
public class Example
{
private static Timer aTimer;
public static void Main()
{
// Create a timer and set a two second interval.
aTimer = new System.Timers.Timer();
aTimer.Interval = 2000;
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
// Have the timer fire repeated events (true is the default)
aTimer.AutoReset = true;
// Start the timer
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program at any time... ");
Console.ReadLine();
}
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
輸出:
Press the Enter key to exit the program at any time... The Elapsed event was raised at 5/20/2015 8:48:58 PM The Elapsed event was raised at 5/20/2015 8:49:00 PM The Elapsed event was raised at 5/20/2015 8:49:02 PM The Elapsed event was raised at 5/20/2015 8:49:04 PM The Elapsed event was raised at 5/20/2015 8:49:06 PM
示例2: Main
//引入命名空間
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
class Program {
static int counter = 0;
static string displayString = "This string will appear one letter at a time. ";
static void Main(string[] args) {
Timer myTimer = new Timer(100);
myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
myTimer.Start();
Console.ReadKey();
}
static void WriteChar(object source, ElapsedEventArgs e) {
Console.Write(displayString[counter++ % displayString.Length]);
}
}