C#两个Timer的应用问题
我在一个Timer1里添加了一个函数(因为需要时间控制运行):
Ceshixiangmumingcheng.Text = Program.mf.TestItems[i];
Ceshixinxiword1.Text = Program.mf.TestItems[i];
Ceshixiangmumingcheng.Visible = true;
Ceshixinxiword1.Visible = true;
CeshixinxiPicture1.Visible = true;
m=0;
Ceshixinxiword2.Text = Program.mf.TestItems[i];
Ceshixiangmumingcheng.Visible = true;
Ceshixinxiword2.Visible = true;
CeshixinxiPicture2.Visible = true;
m=1;
Ceshixinxiword3.Text = Program.mf.TestItems[i];
Ceshixiangmumingcheng.Visible = true;
Ceshixinxiword3.Visible = true;
CeshixinxiPicture3.Visible = true;
m=2;
Ceshixinxiword4.Text = Program.mf.TestItems[i];
Ceshixiangmumingcheng.Visible = true;
Ceshixinxiword4.Visible = true;
CeshixinxiPicture4.Visible = true;
m=3;
然后在第二个Timer2里添加:
private void timer2_Tick(object sender, EventArgs e)
{
switch (m)
{
case 0:
timer1.Interval = 1000;
break;
case 1:
timer1.Interval = 3000;
break;
case 2:
timer1.Interval = 4000;
break;
case 3:
timer1.Interval = 5000;
break;
}
timer2.Stop();
}
但是运行下来是直接一次显示所有,我分步运行看了,他是等Timer1运行完后才运行Timer2的延迟代码,请问我该如何编写才能让我代码里当走打m=一个数字的时候就去Timer2读取个延迟值并且执行呢?
答案:你的两个Timer用的是System.Windows.Forms.Timer?
你试试
System.Timers.Timer
或
System.Threading.Timer
其他:timer 是单线程的 想同时运行 需要调用多线程 winform里的timer都是同步执行的,建议用Thread里的timer 开始运行time1 timer2.stop() ,运行完Timer1 在调用Timer2.Start();
上一个:C# 如何用string.split识别一个算式
下一个:能叫你一声师傅么?我是ASP.NET的初学者,想走开发软件的方向,师傅能不能帮帮我?