timer、timer,你让我头疼。。。

caterpillar12345 2010-10-13 11:20:45


while (stationNum1 < stationSum1)
{
Random rd = new Random();

time1 = rd.Next(5, 10);
timer1.Enabled = true;

Timer t = new Timer();
t.Interval = 1000;

t.Start();

???????????????

bt1_Click(bt1, e);

}

private void timer1_Tick(object sender, EventArgs e)
{
if (time1 > 0)
{
time1--;
label_time_1.Text = time1.ToString();
}
else
{
timer1.Stop();
}
}






就是这么一段代码
怎么才能让执行start之后,在执行完timer.stop()之后再执行bt_Click()事件,执行完再回到while循环?
...全文
190 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
valued_zero 2010-10-14
  • 打赏
  • 举报
回复
stationNum1 和stationSum1是什么意思?
小夏天~ 2010-10-14
  • 打赏
  • 举报
回复
没懂你的意思。
timer1.Stop();

bt1_Click(bt1, e);执行的条件?

while 又是在哪个方法?


明白点
ghost5216 2010-10-14
  • 打赏
  • 举报
回复

using System.Threading;


AutoResetEvent myevent= new AutoResetEvent(false);

while (stationNum1 < stationSum1)
{
Random rd = new Random();

time1 = rd.Next(5, 10);
timer1.Enabled = true;

//Timer t = new Timer();

System.Timers.Timer t = new System.Timers.Timer();
t.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
t.Enabled = true;
t.Interval = 1000;

t.Start();

myevent.WaitOne();
bt1_Click(bt1, e);

}

private void timer1_Tick(object sender, EventArgs e)
{
if (time1 > 0)
{
time1--;
label_time_1.Text = time1.ToString();
}
else
{
timer1.Stop();
}
myevent.Set();
}



xiaotiange 2010-10-14
  • 打赏
  • 举报
回复
学习
hassss2008 2010-10-14
  • 打赏
  • 举报
回复
学习了
湖中仙人 2010-10-14
  • 打赏
  • 举报
回复
将 bt1_Click(bt1, e);
换个地方试试
Jellyfancy 2010-10-14
  • 打赏
  • 举报
回复
这个要用线程吧
caterpillar12345 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyazhe 的回复:]
bt1_Click(bt1, e);

能够在tick的stop那句后面执行。
[/Quote]

这样,start结束之后,不会等到stop,直接再进入到while循环
s8975565 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ghost5216 的回复:]
C# code

using System.Threading;


AutoResetEvent myevent= new AutoResetEvent(false);

while (stationNum1 < stationSum1)
{
Random rd = new Random();

time1……
[/Quote]

AutoResetEvent myevent= new AutoResetEvent(false);
很牛才……
clong320 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyazhe 的回复:]
bt1_Click(bt1, e);

放在tick的stop那句后面执行。

C# code

while (stationNum1 < stationSum1)
{
Random rd = new Random();
time1 = rd.Next(5, 10);
timer1.Enabled = true;
Timer t = n……
[/Quote]
正解,正解
malun666 2010-10-13
  • 打赏
  • 举报
回复
代码我不写了,给你个提示:
伪代码:
定义一个全局变量 bool GoOn = true;

t.Start();
while(GoOn)
{
Thread.sleep(1000);//让线程等等
}
bt1_Click(bt1, e);
在这个方法中添加一句即可

private void timer1_Tick(object sender, EventArgs e)
{
if (time1 > 0)
{
time1--;
label_time_1.Text = time1.ToString();
}
else
{
timer1.Stop();
GoOn = false;
}
}

兔子-顾问 2010-10-13
  • 打赏
  • 举报
回复
bt1_Click(bt1, e);

放在tick的stop那句后面执行。

while (stationNum1 < stationSum1)
{
Random rd = new Random();
time1 = rd.Next(5, 10);
timer1.Enabled = true;
Timer t = new Timer();
t.Interval = 1000;
t.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
if (time1 > 0)
{
time1--;
label_time_1.Text = time1.ToString();
}
else
{
timer1.Stop();
bt1_Click(bt1, e);
}
}
兔子-顾问 2010-10-13
  • 打赏
  • 举报
回复
bt1_Click(bt1, e);

能够在tick的stop那句后面执行。

110,530

社区成员

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

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

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