C#用Timer类在控制台应用程序里怎么实现计时器的每秒更新和显示?
2个回答
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Timer t = new Timer();
t.Elapsed += new ElapsedEventHandler(OnTimedEvent);
t.Interval = 1000;
t.Enabled = true;
Console.Read();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.Clear();
Console.WriteLine(DateTime.Now);
}
}
}
展开全部
decimal i = 0m;
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += (sender, e) =>
{
Console.WriteLine(string.Format("这是第{0}次调用", i));
i++;
};
timer.Start();
Console.ReadLine();
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += (sender, e) =>
{
Console.WriteLine(string.Format("这是第{0}次调用", i));
i++;
};
timer.Start();
Console.ReadLine();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询