关于C#控制中对timer类的使用
2008-06-06 13:51
281 查看
public class Timer2
{
public static void Main()
{
// Create a new Timer with Interval set to 10 seconds.
System.Timers.Timer aTimer = new System.Timers.Timer(10000); //实例化Timer类,设置间隔时间为10000毫秒;
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent); //到达时间的时候执行事件
// Only raise the event the first time Interval elapses.
aTimer.AutoReset = false; //设置是执行一次(false)还是一直执行(true);
aTimer.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
Console.WriteLine("Press /'q/' to quit the sample.");
while(Console.Read()!='q');
}
// Specify what you want to happen when the event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
另外也可以使用让程序休眠的方法Thread.sleep
Thread.Sleep静态方法,使当前线程挂起指定的时间
没有多线程的话,程序只有一个主线程,就是使整个程序休眠
{
public static void Main()
{
// Create a new Timer with Interval set to 10 seconds.
System.Timers.Timer aTimer = new System.Timers.Timer(10000); //实例化Timer类,设置间隔时间为10000毫秒;
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent); //到达时间的时候执行事件
// Only raise the event the first time Interval elapses.
aTimer.AutoReset = false; //设置是执行一次(false)还是一直执行(true);
aTimer.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
Console.WriteLine("Press /'q/' to quit the sample.");
while(Console.Read()!='q');
}
// Specify what you want to happen when the event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
另外也可以使用让程序休眠的方法Thread.sleep
Thread.Sleep静态方法,使当前线程挂起指定的时间
没有多线程的话,程序只有一个主线程,就是使整个程序休眠
相关文章推荐
- 关于C#控制中对timer类的使用
- 关于C#线程中使用定时器timer无效
- 关于c#中timer的使用可能要注意的
- 关于C#多线程、网络编程与计时器Timer的一点使用心得
- C# 关于线程锁lock的使用方法
- C#使用timer定时在屏幕上输出信息的方法
- C# 关于线程锁lock的使用方法
- 使用 Override 和 New 关键字进行版本控制(C# 编程指南)
- 通达OA中 关于msql使用存储过程控制执行的总体成功与否
- 在C#中使用.NET SDK创建控制实例
- (转) C#中Timer使用及解决重入(多线程同时调用callback函数)问题
- C#学习040428关于C#中使用SQL数据库
- C#关于使用Marshal.StructureToPtr保存文件
- C# 关于如何将字符串的地址赋给char*使用
- 关于使用了inline-block导致页面布局不受控制的一点说明
- 在C#中使用COM+实现事务控制
- C#关于AutoResetEvent的使用介绍
- 关于C#中派生类调用基类构造函数的理解 base使用
- 在C#中使用COM+实现事务控制