MrD*_*ase 17 .net c# scheduling timer
我想在用C#编写的简单.NET应用程序中使用计时器.我能找到的唯一一个是Windows.Forms.Timer类.我不想仅为我的控制台应用程序引用此命名空间.
是否有一个C#计时器(或类似计时器)类用于控制台应用程序?
alb*_*ein 29
System.Timers.Timer
正如MagicKat所说:
System.Threading.Timer
你可以在这里看到差异:http: //intellitect.com/system-windows-forms-timer-vs-system-threading-timer-vs-system-timers-timer/
你可以在这里看到MSDN示例:
http://msdn.microsoft.com/en-us/library/system.timers.timer(VS.80).aspx
和这里:
http://msdn.microsoft.com/en-us/library/system.threading.timer(VS.80).aspx
Eri*_*ver 15
我会Timer
在System.Timers
命名空间中推荐这个类.同样有趣的Timer
是,System.Threading
名称空间中的类.
using System;
using System.Timers;
public class Timer1
{
private static Timer aTimer = new System.Timers.Timer(10000);
public static void Main()
{
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
Run Code Online (Sandbox Code Playgroud)
来自MSDN文档的示例.
至少有我所知道的System.Timers.Timer和System.Threading.Timer类.
有一点需要注意(如果你之前没有这样做过),请说你的using子句中已经有了System.Threading命名空间,但实际上你想在System.Timers中使用定时器,你需要这样做:
using System.Threading;
using Timer = System.Timers.Timer;
Run Code Online (Sandbox Code Playgroud)
Jon Skeet在他的多线程指南中有一篇关于Timers的文章,值得一读:http: //www.yoda.arachsys.com/csharp/threads/timers.shtml
归档时间: |
|
查看次数: |
21623 次 |
最近记录: |