就是这样-您如何向C#控制台应用程序添加计时器?如果您可以提供一些示例代码,那将是很好的。
就是这样-您如何向C#控制台应用程序添加计时器?如果您可以提供一些示例代码,那将是很好的。
Answers:
很好,但是为了模拟一段时间,我们需要运行一个需要一些时间的命令,在第二个示例中这非常清楚。
但是,使用for循环永久执行某些功能的样式占用大量设备资源,相反,我们可以使用垃圾回收器来执行类似的操作。
我们可以在同一本书《 CLR via C#Third Ed》的代码中看到此修改。
using System;
using System.Threading;
public static class Program {
public static void Main() {
// Create a Timer object that knows to call our TimerCallback
// method once every 2000 milliseconds.
Timer t = new Timer(TimerCallback, null, 0, 2000);
// Wait for the user to hit <Enter>
Console.ReadLine();
}
private static void TimerCallback(Object o) {
// Display the date/time when this method got called.
Console.WriteLine("In TimerCallback: " + DateTime.Now);
// Force a garbage collection to occur for this demo.
GC.Collect();
}
}
GC.Collect()
。没有什么可收集的。如果GC.KeepAlive(t)
有人称呼它,那将是有道理的Console.ReadLine();
使用System.Threading.Timer类。
System.Windows.Forms.Timer主要设计用于通常在Windows Forms UI线程的单个线程中使用。
在.NET框架的开发初期,还添加了一个System.Timers类。但是,通常建议改用System.Threading.Timer类,因为无论如何这只是System.Threading.Timer的包装。
如果您正在开发Windows服务并且需要计时器定期运行,则还建议始终使用静态(在VB.NET中共享)System.Threading.Timer。这样可以避免定时器对象的垃圾过早收集。
这是控制台应用程序中计时器的示例:
using System;
using System.Threading;
public static class Program
{
public static void Main()
{
Console.WriteLine("Main thread: starting a timer");
Timer t = new Timer(ComputeBoundOp, 5, 0, 2000);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000); // Simulating other work (10 seconds)
t.Dispose(); // Cancel the timer now
}
// This method's signature must match the TimerCallback delegate
private static void ComputeBoundOp(Object state)
{
// This method is executed by a thread pool thread
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000); // Simulates other work (1 second)
// When this method returns, the thread goes back
// to the pool and waits for another task
}
}
摘自杰夫·里希特(Jeff Richter)的《CLR Via C#》。顺便提一下,本书强烈建议您在第23章中介绍这3种计时器背后的原理。
这是创建一个简单的一秒钟计时器刻度的代码:
using System;
using System.Threading;
class TimerExample
{
static public void Tick(Object stateInfo)
{
Console.WriteLine("Tick: {0}", DateTime.Now.ToString("h:mm:ss"));
}
static void Main()
{
TimerCallback callback = new TimerCallback(Tick);
Console.WriteLine("Creating timer: {0}\n",
DateTime.Now.ToString("h:mm:ss"));
// create a one second timer tick
Timer stateTimer = new Timer(callback, null, 0, 1000);
// loop here forever
for (; ; )
{
// add a sleep for 100 mSec to reduce CPU usage
Thread.Sleep(100);
}
}
}
这是结果输出:
c:\temp>timer.exe
Creating timer: 5:22:40
Tick: 5:22:40
Tick: 5:22:41
Tick: 5:22:42
Tick: 5:22:43
Tick: 5:22:44
Tick: 5:22:45
Tick: 5:22:46
Tick: 5:22:47
编辑:将硬自旋循环添加到代码中永远不是一个好主意,因为它们会消耗CPU周期而没有任何收益。在这种情况下,添加该循环只是为了阻止应用程序关闭,从而可以观察线程的操作。但是为了正确起见并减少CPU使用率,在该循环中添加了一个简单的Sleep调用。
让我们玩得开心
using System;
using System.Timers;
namespace TimerExample
{
class Program
{
static Timer timer = new Timer(1000);
static int i = 10;
static void Main(string[] args)
{
timer.Elapsed+=timer_Elapsed;
timer.Start(); Console.Read();
}
private static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
i--;
Console.Clear();
Console.WriteLine("=================================================");
Console.WriteLine(" DEFUSE THE BOMB");
Console.WriteLine("");
Console.WriteLine(" Time Remaining: " + i.ToString());
Console.WriteLine("");
Console.WriteLine("=================================================");
if (i == 0)
{
Console.Clear();
Console.WriteLine("");
Console.WriteLine("==============================================");
Console.WriteLine(" B O O O O O M M M M M ! ! ! !");
Console.WriteLine("");
Console.WriteLine(" G A M E O V E R");
Console.WriteLine("==============================================");
timer.Close();
timer.Dispose();
}
GC.Collect();
}
}
}
或使用简短而甜美的Rx:
static void Main()
{
Observable.Interval(TimeSpan.FromSeconds(10)).Subscribe(t => Console.WriteLine("I am called... {0}", t));
for (; ; ) { }
}
如果您想要更多的控制权,但又可能需要更低的准确性和更多的代码/复杂度,则也可以使用自己的计时机制,但我仍然建议您使用计时器。但是,如果您需要控制实际的计时线程,请使用此命令:
private void ThreadLoop(object callback)
{
while(true)
{
((Delegate) callback).DynamicInvoke(null);
Thread.Sleep(5000);
}
}
将是您的计时线程(将其修改为在需要时停止,并在您希望的任何时间间隔停止)。
并使用/启动您可以执行以下操作:
Thread t = new Thread(new ParameterizedThreadStart(ThreadLoop));
t.Start((Action)CallBack);
回调是要在每个时间间隔调用的void无参数方法。例如:
private void CallBack()
{
//Do Something.
}
您也可以创建自己的(如果对可用选项不满意)。
创建自己的Timer
实现是非常基本的东西。
这是一个应用程序的示例,该应用程序需要在与我的代码库其余部分相同的线程上访问COM对象。
/// <summary>
/// Internal timer for window.setTimeout() and window.setInterval().
/// This is to ensure that async calls always run on the same thread.
/// </summary>
public class Timer : IDisposable {
public void Tick()
{
if (Enabled && Environment.TickCount >= nextTick)
{
Callback.Invoke(this, null);
nextTick = Environment.TickCount + Interval;
}
}
private int nextTick = 0;
public void Start()
{
this.Enabled = true;
Interval = interval;
}
public void Stop()
{
this.Enabled = false;
}
public event EventHandler Callback;
public bool Enabled = false;
private int interval = 1000;
public int Interval
{
get { return interval; }
set { interval = value; nextTick = Environment.TickCount + interval; }
}
public void Dispose()
{
this.Callback = null;
this.Stop();
}
}
您可以添加事件,如下所示:
Timer timer = new Timer();
timer.Callback += delegate
{
if (once) { timer.Enabled = false; }
Callback.execute(callbackId, args);
};
timer.Enabled = true;
timer.Interval = ms;
timer.Start();
Window.timers.Add(Environment.TickCount, timer);
为了确保计时器正常工作,您需要创建一个无限循环,如下所示:
while (true) {
// Create a new list in case a new timer
// is added/removed during a callback.
foreach (Timer timer in new List<Timer>(timers.Values))
{
timer.Tick();
}
}
你有它 :)
public static void Main()
{
SetTimer();
Console.WriteLine("\nPress the Enter key to exit the application...\n");
Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
Console.ReadLine();
aTimer.Stop();
aTimer.Dispose();
Console.WriteLine("Terminating the application...");
}
private static void SetTimer()
{
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(2000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
e.SignalTime);
}
我建议您遵循Microsoft准则( https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer.interval?view=netcore-3.1)。
我第一次尝试使用System.Threading;
与
var myTimer = new Timer((e) =>
{
// Code
}, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
但它在约20分钟后连续停止。
这样,我尝试了解决方案设置
GC.KeepAlive(myTimer)
要么
for (; ; ) { }
}
但在我的情况下,它们不起作用。
遵循Microsoft文档,它可以完美运行:
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);
}
}
// The example displays output like the following:
// 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