110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Text;
using System.Timers;
class test
{
//已经等待的时间
static int waittime = 0;
static void Main()
{
Timer timer1 = new Timer();
//每隔100毫秒监测是否到了自动退出时间
timer1.Interval = 100;
timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer1.Start();
Console.WriteLine("请输入按键");
while (true)
{
waittime = 0;
ConsoleKeyInfo cki = Console.ReadKey();
Console.WriteLine("\n------------------你输入的字符是:"+cki.KeyChar);
}
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
waittime += 100;
//DEBUG,看看计时器工作是否正常
//Console.WriteLine("当前等待时间:" + waittime);
//3000毫秒即3秒,若无操作3秒后自动退出
if (waittime> 3000)
{
Console.WriteLine("按键超时,自动退出");
//退出控制台应用程序
Environment.Exit(0);
}
}
}