Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; //延迟函数必备
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DateTime dt = new DateTime();
bool ToBeContinue=true;
while (ToBeContinue)
{
ConsoleKeyInfo keyInfo;
if (System.Console.KeyAvailable)
{
keyInfo = System.Console.ReadKey(true);
//if (keyInfo.Key.ToString()=="Enter")//如果按了Enter键
if (keyInfo.KeyChar == (char)027)//如果按了Esc键,Esc键的ASCII码为十进制27
ToBeContinue = false;
}
else//没有按Esc键,时间一秒一秒走动
{
dt = DateTime.Now;//获取当前时间
Thread.Sleep(1000);//延迟1秒
Console.Clear();
Console.WriteLine(dt);
}
}
Console.WriteLine("你已经按了Esc键退出,时间静止!");
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; //延迟函数必备
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DateTime dt = new DateTime();
bool ToBeContinue=true;
while (ToBeContinue)
{
ConsoleKeyInfo keyInfo;
if (System.Console.KeyAvailable)
{
keyInfo = System.Console.ReadKey(true);
//if (keyInfo.Key.ToString()=="Enter")//如果按了Enter键
if (keyInfo.KeyChar == (char)027)//如果按了Esc键,Esc键的ASCII码为十进制27
ToBeContinue = false;
}
else//没有按Esc键,时间一秒一秒走动
{
dt = DateTime.Now;//获取当前时间
Thread.Sleep(1000);//延迟1秒
Console.Clear();
Console.WriteLine(dt);
}
}
Console.WriteLine("你已经按了Esc键退出,时间静止!");
Console.ReadKey();
}
}
}