BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #17005同步于 2007/4/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

C#中启动多个timer的问题

wiwi
2007/4/23镜像同步8 回复
怎样在c#中启动多个timer? 不是说只是启动两个或者几个timer的问题,是想通过数组启动多个timer,然后不同的timer有不同的interval和触发事件。 测试代码如下: //代码 using System; using System.Timers; namespace TimerTest { class Class1 { [STAThread] static void Main(string[] args) { TimerTest[] tt = new TimerTest[3]; for(int i = 1; i < 3; i++) { tt[i] = new TimerTest(); tt[i].Timer1(i,i); } } } public class TimerTest { private static System.Timers.Timer aTimer = new System.Timers.Timer(); private static string info; private static int a; public TimerTest() { } public void Timer1(int i, int a1) { a = a1; info = "This is the " + i.ToString() + "th timer"; aTimer.Interval = i*1000; aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed); aTimer.Enabled = true; while(a<4); } private void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Console.WriteLine(info); a++; } } } //end 现在输出为: this is the 1th timer this is the 1th timer this is the 1th timer this is the 2th timer this is the 2th timer 但实际上想实现为: this is the 1th timer this is the 1th timer this is the 2th timer this is the 1th timer this is the 2th timer 问题是只有运行完第一个循环後才会运行第二个循环,也就是说只有打印完timer 1之后才会去打印timer 2。想实现两个同时计时,不知道应该怎样实现?
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
wiwi机器人#1 · 2007/4/23
【 在 wiwi 的大作中提到: 】 : 怎样在c#中启动多个timer? : 不是说只是启动两个或者几个timer的问题,是想通过数组启动多个timer,然后不同的timer有不同的interval和触发事件。 : 测试代码如下: : ................... 下面这段代码能实现我所实现的功能,但不能够由数组定义 using System; using System.Timers; namespace test1 { class Class1 { static void Main(string[] args) { System.Timers.Timer stt1 = new Timer(); System.Timers.Timer stt2 = new Timer(); stt1.Enabled = true; stt2.Enabled = true; stt1.Interval = 1000; stt2.Interval = 2000; stt1.Elapsed += new ElapsedEventHandler(stt1_Elapsed); stt2.Elapsed += new ElapsedEventHandler(stt2_Elapsed); while(true); } private static void stt1_Elapsed(object sender, ElapsedEventArgs e) { Console.WriteLine("This is the 1th timer"); } private static void stt2_Elapsed(object sender, ElapsedEventArgs e) { Console.WriteLine("This is the 2th timer"); } } }
wiwi机器人#2 · 2007/4/23
还有一种写法,但不知怎么写Class1_Elapsed方法 using System; using System.Timers; namespace test1 { class Class1 { System.Timers.Timer[] stt = new Timer[4]; static void Main(string[] args) { Class1 c1 = new Class1(); for(int i = 1; i < 4; i ++) { c1.stt[i] = new Timer(); } for(int i = 1; i < 4; i++) { c1.stt[i].Enabled = true; } for(int i = 1; i < 4; i++) { c1.stt[i].Interval = i*1000; } for(int i = 1; i < 4; i++) { c1.stt[i].Elapsed += new ElapsedEventHandler(Class1_Elapsed); } while(true); } private static void Class1_Elapsed(object sender, ElapsedEventArgs e) { } } }
reniking机器人#3 · 2007/4/24
没想出怎么解决,lz解决了别忘了把方法发上来哦 有一个想法,但是没有实现,对于2楼中的elapsed方法,可不可以用object sender这个参数确定来确定是哪个timer在调用这个方法呢
wardensky机器人#4 · 2007/4/24
【 在 reniking 的大作中提到: 】 : 没想出怎么解决,lz解决了别忘了把方法发上来哦 : 有一个想法,但是没有实现,对于2楼中的elapsed方法,可不可以用object sender这个参数确定来确定是哪个timer在调用这个方法呢 不行,他只有4个参数,但无法确定是那个timer,只能确定timer类型
Lonhero机器人#5 · 2007/4/24
帮你写了一个o(∩_∩)o... using System; namespace TimerDemo { class Program { [STAThread] static void Main(string[] args) { TimerDemo[] td = new TimerDemo[3]; for (int i = 1; i < 3; i++) { td[i] = new TimerDemo(i,i); td[i].TimerStart(); } Console.WriteLine(); Console.ReadLine(); } public class TimerDemo { private int timerInterval; private int eventSelect; /// <summary> /// 构造函数 /// </summary> /// <param name="eventSelect">事件选择</param> /// <param name="timerInterval">时间间隔</param> public TimerDemo(int eventSelect,int timerInterval) { this.eventSelect = eventSelect; this.timerInterval = timerInterval; Console.WriteLine("TimerDemo Start,eventSelect is " + eventSelect.ToString() + "timerInterval is " + timerInterval.ToString()); } public void TimerStart() { System.Timers.Timer t = new System.Timers.Timer(); Console.WriteLine(timerInterval.ToString() + " TimerStart"); t.Interval = timerInterval * 1000; t.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed); t.Enabled = true; } public void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Console.WriteLine("This is the " + timerInterval.ToString() + "th timer"); if (eventSelect > 1) doB(); else doA(); Console.WriteLine(); } private void doA() { Console.WriteLine("Hi,I'm doing A..."); } private void doB() { Console.WriteLine("Hi,I'm doing B..."); } } } }
TimNew机器人#6 · 2007/4/24
C#里面总共有两种内置Timer(我指的是Managed的版本,不包括系统核心的Timer和API提供的那两个) 一个是以Component形式出现的,一个是Control形式出现的。 LZ可以使用那两个,LS几个通过的Demo都是用那两个东东弄的。 如果LZ希望自己的第一代码可以通过,尝试一下Thread,把每个时钟的Timer1放到一个Thead里去,就可以了
Lonhero机器人#7 · 2007/4/24
LS的,C#里总共有3种Timer,谢谢。 PS:看来要注意加强基本功了哦 o(∩_∩)o... BTW,做Demo只是简单引导并告诉别人该怎么做。
TimNew机器人#8 · 2007/4/25
【 在 Lonhero 的大作中提到: 】 : LS的,C#里总共有3种Timer,谢谢。 : PS:看来要注意加强基本功了哦 o(∩_∩)o... : BTW,做Demo只是简单引导并告诉别人该怎么做。 我说了Timer是Managed的版本,不包括API和内核的版本。 System.Threading.Timer其实是一个API的版本,你可以把System.Threading.dll,ILDASM后看看。