你怎么一个计时器添加到C#控制台应用程序 [英] How do you add a timer to a C# console application
问题描述
就在这个 - 你怎么一个计时器添加到C#控制台应用程序?这将是巨大的,如果你能提供一些例如编码。
Just this - How do you add a timer to a C# console application? It would be great if you could supply some example coding.
推荐答案
这是非常好的,但为了模拟一些时间的推移,我们需要运行需要一些时间,这就是在第二个例子中很清楚的命令。
That's very nice, however in order to simulate some time passing we need to run a command that takes some time and that's very clear in second example.
然而,使用一个for循环永远做一些功能性的风格需要大量的设备资源,相反,我们可以使用垃圾收集器做一些事情这样的。
However, the style of using a for loop to do some functionality forever takes a lot of device resources and instead we can use the Garbage Collector to do some thing like that.
我们可以看到在code这个修改从同一本书CLR通过C#第三版。
We can see this modification in the code from the same book 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();
}
}
这篇关于你怎么一个计时器添加到C#控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!