如何实现半秒或更短时间的延时?

daisylong 2010-12-08 11:26:34
我要实现一个功能:点击一个“显示1”按键,textbox1中显示个“1”,半秒钟后(或更短的时间如200毫秒)1消失变成0,再按显示按键重复。同时,还有另外两个按键“显示2”与“显示3”,也是实现同样的功能。
由于Timer组件一般用来做秒计时,而我现在是毫秒的计算,不知Timer能不能实现?或不用Timer有什么好的方法?求详解!
...全文
258 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-12-08
  • 打赏
  • 举报
回复
System.Threading.Timer t = new System.Threading.Timer(new TimerCallback(test1));
t.Change(1000, 1000);
private void test1(object state)
{
System.Threading.Timer t = (System.Threading.Timer)state;
t.Dispose();
//开始
System.Threading.Timer tt = new System.Threading.Timer(new TimerCallback(test2));
tt.Change(1000, Timeout.Infinite);
}

daisylong 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sprc_lcl 的回复:]
C# code
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "1";

Thread thr = new Thread(new ThreadStart(Run));
thr……
[/Quote]
嗯,我有用过这个,试一下
daisylong 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 adsdassadfasdfasdf 的回复:]
上面的是用js的!

个人感觉,还是js用着舒服!
[/Quote]
没有C#的代码吗?
sprc_lcl 2010-12-08
  • 打赏
  • 举报
回复
        private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "1";

Thread thr = new Thread(new ThreadStart(Run));
thr.IsBackground = true;
thr.Start();
}

private void Run()
{
Thread.Sleep(200);//休息200毫秒
this.textBox1.Invoke(new MethodInvoker(() => { this.textBox1.Text = "0"; }));
}
一克代码 2010-12-08
  • 打赏
  • 举报
回复
上面的是用js的!

个人感觉,还是js用着舒服!
一克代码 2010-12-08
  • 打赏
  • 举报
回复
给一个我测试过的!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function showtime() {
document.getElementById("textbox1").value="1";
setTimeout("showtime1()",500); //500是500毫秒

}
function showtime1() {
document.getElementById("textbox1").value="0";

}
</script>
</head>
<body>
<input type="text" id="textbox1" size="23" >
<input name="" type="button" value=" 保 存 " onclick="showtime()"/>
</body>
</html>
一克代码 2010-12-08
  • 打赏
  • 举报
回复
用timer控制啊!

它本身就是毫秒级的!

你在onclick事件里面用!
function showtime() {
document.getElementById("textbox1").value="1";
if(timerID = setTimeout("showtime1()",500); 500是500毫秒

}
function showtime1() {
document.getElementById("textbox1").value="0";

}
baihualin1983 2010-12-08
  • 打赏
  • 举报
回复
一般都用timer,它本身就是毫秒级的
Mr_graceless 2010-12-08
  • 打赏
  • 举报
回复
上面的方法都可以。。
一克代码 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 daisylong 的回复:]
引用 10 楼 adsdassadfasdfasdf 的回复:
那就用js不就行了!

那你上面说的onClick事件是在按键的吗?这个不熟悉,还请详细指教啊
[/Quote]

是啊!

你把我写的代码弄得html里面,看看效果就知道!
daisylong 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 adsdassadfasdfasdf 的回复:]
那就用js不就行了!
[/Quote]
那你上面说的onClick事件是在按键的吗?这个不熟悉,还请详细指教啊
一克代码 2010-12-08
  • 打赏
  • 举报
回复
那就用js不就行了!

daisylong 2010-12-08
  • 打赏
  • 举报
回复
还要强调一点,我的屏幕还有一个主时间倒计时60S,所以不能延时太长,因为不能引起主时间的延时

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧