当前位置:首页 > Windows程序 > 正文

C# 计时器和计数

2024-03-31 Windows程序

定义:
System.Threading.Timer timer;
int count;
TextBox textBox1;

创建计时器和每秒要执行的方法:
timer = new System.Threading.Timer(st =>
{
++count;
textBox1.AppendText("计数:" + count.ToString() + "\n");
if (count == 100)
timer.Change(Timeout.Infinite, Timeout.Infinite);
},null, Timeout.Infinite, Timeout.Infinite);
或(callback是执行函数):
timer = new System.Threading.Timer(callback,null, 1000, 500); (毫秒)
private void Callback( Object state )
{
// 执行操作,执行完归零计时器,回调callback,如此循环
timer .Change( 1000, 500);
}

启动:
textBox1.AppendText("开始\n");
count = 0;
timer.Change(0, 1000);

暂停:
timer.Change(Timeout.Infinite, Timeout.Infinite);

继续:
timer.Change(0, 1000);

停止:
timer.Change(Timeout.Infinite, Timeout.Infinite);
count = 0;

标签:

原文地址:https://www.cnblogs.com/wa502/p/11603458.html

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/9104.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494585访问次数
  • 建站天数
  • 友情链接