[C#基礎]-C#實做Console Timer功能
[C#基礎]-C#實做Console Timer功能
本篇要分享C#實做Console Timer功能範例,有興趣的同好,歡迎來一下哈哈 ^ ^。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
namespace ConsoleTimer
{
class Program
{
static public void ShowNowTime()
{
Console.WriteLine(DateTime.Now.ToString());
Console.WriteLine("Press any key to close App . . . ");
}
static void Main(string[] args)
{
System.Timers.Timer tmr = new System.Timers.Timer(1000);
tmr.Elapsed += delegate
{
ShowNowTime();
};
tmr.Start();
Console.ReadKey(true);
}
}
}
|
2 thoughts on “[C#基礎]-C#實做Console Timer功能”
//.net 6 測試 OK
// https://stackoverflow.com/questions/186084/how-do-you-add-a-timer-to-a-c-sharp-console-application
using System;
using System.Threading;
public class Program
{
private static Timer m_timer = null;
public static void Main()
{
// Create a Timer object that knows to call our TimerCallback
// method once every 2000 milliseconds.
m_timer = new Timer(TimerCallback, null, 0, 2000);
// Wait for the user to hit
Console.ReadLine();
}
private static void TimerCallback(Object o)
{
// Display the date/time when this method got called.
Console.WriteLine(“In TimerCallback: ” + DateTime.Now);
}
}
原本程式 在.net 6 也測試OK