C# timer定時在屏幕上輸出信息的代碼

likeo 9年前發布 | 840 次閱讀 C#

這段c#代碼通過timer定時器每隔5秒鐘調用一次OnTimerElapsed事件,在屏幕上輸出信息,這是一個簡單的timer定時器使用范例,可以大概了解一些C#中timer的用法

using System;
using System.Timers;

public class Program { private static System.Timers.Timer testTimer;

public static void Main(string[] args)
{
    testTimer = new System.Timers.Timer(5000); // 5 seconds
    testTimer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);

    testTimer.Interval = 5000;
    testTimer.Enabled = true;

    Console.WriteLine("Press the enter key to stop the timer");
    Console.ReadLine();

}

private static void OnTimerElapsed(object source, ElapsedEventArgs e)
{
    Console.WriteLine("Timer elapsed at {0}", e.SignalTime);
}

}</pre>

 本文由用戶 likeo 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!