C#实现倒计时的功能
发布网友
发布时间:2022-05-11 01:05
我来回答
共2个回答
热心网友
时间:2022-04-23 09:44
C#实现倒计时功能,参考代码如下:
int Timecount = 0;//记录倒计时总毫秒数
int isstop = 0;//标示是否启动/暂停的变量,0表示第一次初始化
private void button1_Click(object sender, EventArgs e)
{
if (this.button1.Text == "开始计时" || this.button1.Text == "继续计时")
{
this.button1.Text = "暂停计时";
//this.timer1.Interval = 1;
//this.timer1.Start();
if (isstop == 0)//第一次执行或者倒计时事件设置发生变化,则重新倒计时
{
Timecount = Convert.ToInt32(txtTotalmm.Text) * 60000;//毫秒
Thread counter = new Thread(Counter);
counter.Start();
Control.CheckForIllegalCrossThreadCalls = false;//放弃捕获对错误线程的调用,否则在线程中无法调用控件名
this.txtTotalmm.ReadOnly = true;
this.button2.Visible = false;
txthour.ForeColor = Color.Black;
txtmm.ForeColor = Color.Black;
txtss.ForeColor = Color.Black;
txtmss.ForeColor = Color.Black;
}
isstop = 1;//启动
}
else
{
this.button1.Text = "继续计时";
//this.timer1.Stop();
isstop = 2;//暂停
}
}
public void Counter()
{
try
{
while (Timecount >= 0)
{
this.txthour.Text = (Timecount / 3600000).ToString();
this.txtmm.Text = ((Timecount / 60000) % 60).ToString();
this.txtss.Text = ((Timecount / 1000) % 60).ToString();
this.txtmss.Text = (Timecount % 1000).ToString();
//label1.Text = hour.ToString() + "时 " + minute.ToString() + "分 " + second.ToString() + "秒" + millsecond + "毫秒";
if (Timecount == 0)
{
txthour.ForeColor = Color.Red;
txtmm.ForeColor = Color.Red;
txtss.ForeColor = Color.Red;
txtmss.ForeColor = Color.Red;
this.txtTotalmm.ReadOnly = false;
this.button2.Visible = true;
this.button1.Text = "开始计时";
isstop = 0;
try
{
Thread currthread = Thread.CurrentThread;
currthread.Abort();// 终止当前进程,会触发ThreadAbortException异常,从而终止进程,所以下面需要捕获该异常才能终止进程
}
catch (ThreadAbortException) { }
}
if (isstop != 2)
Timecount -= 1;
Thread.Sleep(1);
}
}
catch { }//处理异常关闭情况下的异常问题
}
private void button2_Click(object sender, EventArgs e)
{
this.txtTotalmm.Text = (Convert.ToInt32(this.txtTotalmm.Text) + 1).ToString();
}
//Timer控件的Interval频率值小于17时便会产生误差,所有得到的倒计时秒数比普通的慢很多,当倒计时精确到毫秒时,则不适用用Timer控件。
//private void timer1_Tick(object sender, EventArgs e)
//{
// if (Timecount >= 0)
// {
// this.txthour.Text = (Timecount / 3600000).ToString();
// this.txtmm.Text = ((Timecount / 60000) % 60).ToString();
// this.txtss.Text = ((Timecount / 1000) % 60).ToString();
// this.txtmss.Text = (Timecount % 1000).ToString();
// //label1.Text = hour.ToString() + "时 " + minute.ToString() + "分 " + second.ToString() + "秒" + millsecond + "毫秒";
// if (Timecount == 0)
// {
// txthour.ForeColor = Color.Red;
// txtmm.ForeColor = Color.Red;
// txtss.ForeColor = Color.Red;
// txtmss.ForeColor = Color.Red;
// }
// Timecount -= 10;
// }
//}
注意:
1.倒计时如果是以秒为单位,可以选择Timer和线程来实现,如果以毫秒为单位,建议使用线程来实现。
2.使用线程时,如果要暂停,需要使用Abort()方法终止线程,该方法是通过触发ThreadAbortException异常来实现对线程的终止的,所以需要捕获该异常,才能终止线程。所以 Abort()引发的该异常时正常的。
热心网友
时间:2022-04-23 11:02
刚好有空 给你写了一个 代码:using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;namespace WinForm_test_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} int hour = 12;
int minute = 0;
int second = 0;
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(Func);
thread.Start();
} private void Func()
{
while (hour > 0)
{
this.Invoke((EventHandler)delegate {
if (second < 0)
{
second = 59;
minute--; }
if (minute < 0)
{
minute = 59;
hour--;
}
label1.Text = "剩余时间:" + hour.ToString() + "小时 " + minute.ToString() + "分钟 " + second.ToString() + "秒";
});
Thread.Sleep(1000);
second--;
}
} }
}
有不明白的地方请追问.