📄 sample48.cs
字号:
namespace apiBook
{
using System;
using System.Timers;
public class TestTimerClass
{
static int i=0;
public static void Main()
{
Timer testT1 = new Timer();
testT1.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
testT1.Interval=1000;
testT1.Enabled=true;
Console.WriteLine("下面这个是1秒显示一次(注意频率),按q使它停下");
while(Console.Read()!='q'&&Console.Read()!='Q'&&i!=8);
testT1.Start();
//使用Start方法引发事件
Console.WriteLine("如果要它停下,请按r,谢谢");
while(Console.Read()!='r');
testT1.Stop();
//使用Stop方法停止该事件
Timer testT2 = new Timer(5000);
Console.WriteLine("下面这个是5秒显示一次(注意频率)");
testT2.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
testT2.AutoReset = false;
testT2.Enabled = false;
Console.WriteLine("10秒的对象AutoReset = false");
Console.WriteLine("请等10秒后,按e启动5秒的对象,谢谢");
while(Console.Read()!='e');
Console.WriteLine("执行Start操作");
Console.WriteLine("5秒的对象执行Start");
testT2.Start();
Console.WriteLine("请等待10秒后确定它只显示一次,请按w,看它AutoReset=true有什么不同");
while(Console.Read()!='w');
Console.WriteLine("等等,如果你看烦这循环,请按q退出程序");
testT2.AutoReset=true;
//设置属性AutoReset
while(Console.Read()!='q');
}
public static void OnTimedEvent(object obj, ElapsedEventArgs e)
{
Console.WriteLine(e.SignalTime+" , "+i);
i++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -