计时精度到底毫秒级别或者更高 如何使用C#制作一个精确计时器

本文介绍了一个使用C#编写的简单秒级计时器应用,包括正计时和倒计时功能。提供了完整的TIME类定义及主窗体中按钮事件处理逻辑,展示了如何根据当前时间初始化计时器,并通过按钮触发计时开始与停止。
代码如下:Private Declare Function GetTickCount Lib "kernel32" () As Long Public SStart EventArgs e) { time.IsInc = true; time.IsDec = false; } private void decrease_btn_Click(object sender EventArgs e) { if (time.IsInc) time.incease(); else { time.decrease(); } label1.Text = time.Hour.ToString() + ":" + time.Minute.ToString() + ":" + time.Second.ToString(); } private void increase_btn_Click(object sender EventArgs e) { //this.getList(); time = new TIME(); time.Second = int.Parse(System.DateTime.Now.Second.ToString()); time.Minute = int.Parse(System.DateTime.Now.Minute.ToString()); time.Hour = int.Parse(System.DateTime.Now.Hour.ToString()); } private void timer1_Tick(object sender 写一个TIME类 代码: class TIME { private int hour; private int minute; private int second; private bool isInc; private bool isDec; public int Hour { get { return this.hour; } set { this.hour = value; } } public int Minute { get { return this.minute; } set { this.minute = value; } } public int Second { get { return this.second; } set { this.second = value; } } public bool IsInc { get { return this.isInc; } set { this.isInc = value; } } public bool IsDec { get { return this.isDec; } set { this.isDec = value; } } public void incease() { if (this.Second < 60) this.Second++; else { if (this.minute < 60) this.minute++; else { this.hour++; } } } public void decrease() { if (this.Second > 0) this.Second--; else { if (this.minute > 0) this.minute--; else { this.minute = 59; this.hour--; } } } }主窗体中的代码: TIME time; private void Form1_Load(object sender Stime Private Sub Command1_Click() Command1.Enabled = False: Command2.Enabled = True: Command2.Default = True SStart = GetTickCount Text1.Text = Text1.Text & "你点击[开始]的时间:" & Now & vbCrLf End Sub Private Sub Command2_Click() SEnd = GetTickCount Stime = Val(SEnd - SStart) Text1.Text = Text1.Text & "你点击[停止]的时间:" & Now & vbCrLf & "已经过时间:" & Stime / 1000 & "秒" & Stime Mod 1000 & "毫秒" & vbCrLf End Sub Private Sub Form_Load() Command1.Caption = "开始": Command2.Caption = "停止": Command2.Enabled = False: Text1.Text = "": Command1.Default = True End Sub ||| ytu77u SEnd EventArgs e) { time.IsDec = true; time.IsInc = false; }这个实现的是现在以秒为单位的倒计时 你可以根据需要调节TIME的INTERVAL属性来实现你需要的功能 ||| 终于写完了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值