CountdownClock.AlarmEventHandler(RaiseAlarm); } public void RaiseAlarm(CountdownClock cc,EventArgs e) { Console.WriteLine(在{0}引发警报,cc.ToString()); } } 公共类测试员 { static void Main() { Tester t = new Tester(); t.Start(); } public void Start () { 闹钟beep =新闹钟(); CountdownClock cntDownClock = new CountdownClock(); beep.Subscribe(cntDownClock); cntDownClock.Run(); } } }Hello, I am having trouble with the following console application. Iam using delegates/event to make a clock raise an alarm when a certainamount of time has been reached. The following compiles, but code doesnothing else then to produce an endless loop. Any help would begreatly appreciated, I''m probably overlooking something silly.Regards,Arjen.The code:using System;using System.Threading;namespace ArjenCsharp{public class CountdownClock //doesn''t actually count down ;){private int hour;private int minute;private int second;public int Hour{get { return hour; }set { hour = value; }}public int Minute{get { return minute; }set { minute = value; }}public int Second{get { return second; }set { second = value; }}public bool Equals(CountdownClock theClock){if ((this.hour == theClock.hour) && (this.minute ==theClock.minute) && (this.second == theClock.second))return true;elsereturn false;}public override string ToString(){return this.Hour.ToString() + ":" + this.Minute.ToString()+ ":" + this.Second.ToString();}public CountdownClock(int theHour, int theMinute, inttheSecond){hour = theHour;minute = theMinute;second = theSecond;}//default constructor - takes no actionpublic CountdownClock(){}//the delegatepublic delegate void AlarmEventHandler(CountdownClock o,EventArgs e);//public instance of the delagatepublic event AlarmEventHandler myAlarmDelegate;public void Run(){DateTime dt = DateTime.Now;CountdownClock targetTime = new CountdownClock(dt.Hour,dt.Minute+2, dt.Second);for (;;){Thread.Sleep(100);DateTime updateTime = DateTime.Now;if (this.Equals(targetTime)){myAlarmDelegate(targetTime, EventArgs.Empty);}this.Hour = updateTime.Hour;this.Minute = updateTime.Minute;this.Second = updateTime.Second;}}}public class Alarm{public void Subscribe(CountdownClock theClock){theClock.myAlarmDelegate += newCountdownClock.AlarmEventHandler(RaiseAlarm);}public void RaiseAlarm(CountdownClock cc, EventArgs e){Console.WriteLine("Alarm raised at {0}", cc.ToString());}}public class Tester{static void Main(){Tester t = new Tester();t.Start();}public void Start(){Alarm beep = new Alarm();CountdownClock cntDownClock = new CountdownClock();beep.Subscribe(cntDownClock);cntDownClock.Run();}}}推荐答案 On Thu ,2007年5月17日15:30:12 +0200,Snowbleach< ar ********* @ gmail.comwrote:On Thu, 17 May 2007 15:30:12 +0200, Snowbleach <ar*********@gmail.comwrote: 您好,我是在以下控制台应用程序中遇到问题。我/ b 使用代表/事件让时钟在达到某个 时间时发出警报。以下编译,但代码确实没有其他任何东西产生无限循环。任何帮助都会非常感激,我可能会忽略一些愚蠢的东西。Hello, I am having trouble with the following console application. Iam using delegates/event to make a clock raise an alarm when a certainamount of time has been reached. The following compiles, but code doesnothing else then to produce an endless loop. Any help would begreatly appreciated, I''m probably overlooking something silly. 嗯,代码为我编译并确实在目标时间生效时输出到控制台的行。然而,在发生这种情况之后,你永远不会退出循环,导致它永远运行,或者至少在输出新行之前整整24小时,然后24小时...... 尝试加入休息;触发事件后。 myAlarmDelegate(targetTime,EventArgs.Empty); 休息; 提示:当字符串连接时,如果连接的任何组件是字符串文字,则会自动在所有其他组件上自动调用ToString()。除了可读性之外,不需要自己动手。 - 快乐的编码! Morten Wennevik [C#MVP ]Well, the code compiles for me and does indeed output lines to the console while the target time is effective. However, after this has happened you never exit the loop causing it to run forever, or at least for a full 24 hours before outputting new lines, and then 24 hours ...Try adding break; after firing the event.myAlarmDelegate(targetTime, EventArgs.Empty);break;Hint: when string concatenating, if any of the components being concatenated are string literals, ToString() will automatically be called on all the other components automatically. No need to do it yourself, other than perhaps readability.--Happy coding!Morten Wennevik [C# MVP] " Morten Wennevik [C#MVP]" < Mo ************ @ hotmail.comwrote in message news:op.tsg432mvdj93y5@stone ..."Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in messagenews:op.tsg432mvdj93y5@stone... 2007年5月17日星期四15:30:12 +0200,Snowbleach< ar ********* @ gmail.com> 写道:On Thu, 17 May 2007 15:30:12 +0200, Snowbleach <ar*********@gmail.com>wrote: >您好,我在使用以下控制台应用程序时遇到问题。我正在使用代表/活动让时钟在达到一定的时间后发出警报。以下编译,但代码确实没有别的产生无限循环。非常感谢任何帮助,我可能会忽略一些愚蠢的东西。>Hello, I am having trouble with the following console application. Iam using delegates/event to make a clock raise an alarm when a certainamount of time has been reached. The following compiles, but code doesnothing else then to produce an endless loop. Any help would begreatly appreciated, I''m probably overlooking something silly. 嗯,代码为我编译并确实输出行到控制台 而目标时间有效。然而,在发生这种情况之后你就不会退出循环,导致它永远运行,或者至少在输出新线路之前完全24小时b $ b小时,然后24小时...Well, the code compiles for me and does indeed output lines to the consolewhile the target time is effective. However, after this has happened younever exit the loop causing it to run forever, or at least for a full 24hours before outputting new lines, and then 24 hours ... 此外,不要检查警报时间是否相等,检查 是否超过警报时间(然后提前你的闹钟时间,这样你就不会连续开火了。您的应用程序可能会在一秒钟的间隔内睡眠,其间的时间实际上相等。And additionally, don''t check for equality to the alarm time, check forbeing past the alarm time (then advance your alarm time so you don''t firecontinuously). It''s possibile for your app to sleep for the one-secondinterval where the times would actually compare equal. > 尝试添加休息;触发事件后。 myAlarmDelegate(targetTime,EventArgs.Empty); 休息; 提示:当字符串连接时,如果连接的任何组件是字符串文字,ToString()将自动在所有其他组件上调用 。不需要自己做, 除了可读性之外。>Try adding break; after firing the event.myAlarmDelegate(targetTime, EventArgs.Empty);break;Hint: when string concatenating, if any of the components beingconcatenated are string literals, ToString() will automatically be calledon all the other components automatically. No need to do it yourself,other than perhaps readability. 实际上,使用显式ToString()可以节省值类型的装箱费用, 当然如果你想要格式字符串你需要明确致电 ToString。Actually, putting explicit ToString() saves boxing overhead for value types,and of course if you want format strings you need to explicitly callToString. > - 快乐的编码! Morten Wennevik [C#MVP]>--Happy coding!Morten Wennevik [C# MVP]谢谢你们的帮助。它帮助了很多。我不知道我的代码 在发布时确实工作了。我看到一个黑屏和 认为我的代码坏了,但因为我从未在 中显示时间cntDownClock.Run()我不知道代码是什么这样做。另外, 认为我的代码坏了我终止了应用程序,然后才能达到 警报时间,呵呵......好吧,另一个宝贵的教训 学到了! 无论如何,这里是新修改的Run()方法,实现了你的建议: public void Run() { DateTime dt = DateTime.Now; CountdownClock targetTime = new CountdownClock(dt.Hour, dt.Minute + 1,dt.Second); // targettime = 1分钟 for(;;) { Thread.Sleep(100); DateTime updateTime = DateTime.Now; if(updateTime.Minute> = targetTime.Minute) { myAlarmDelegate(targetTime,EventArgs.Empty); targetTime.Minute + = 1; 休息; } //显示每秒的时间 if(updateTime.Second!= second) { Console.WriteLine(" ; {0}:{1}:{2}", this.Hour.ToString(),this.Minute.ToString(),this.Second.ToString()); } this.Hour = updateTime.Hour; this.Minute = updateTime.Minute; this.Second = updateTime.Second; } }Thank you both for your help. It helped a lot. I wasn''t aware my codeat the time of posting did in fact work. I saw a black screen andfigured my code was broken, but since I never displayed the time inthe cntDownClock.Run() I had no idea what the code was doing. Plus,thinking my code was broken I terminated the application before thealarm time could be reached, heh.. Well, another valuable lessonlearned!Anyhow, here''s the new modified Run() method, implementing youradvice:public void Run(){DateTime dt = DateTime.Now;CountdownClock targetTime = new CountdownClock(dt.Hour,dt.Minute+1, dt.Second); //targettime = 1 minutefor (;;){Thread.Sleep(100);DateTime updateTime = DateTime.Now;if (updateTime.Minute >= targetTime.Minute){myAlarmDelegate(targetTime, EventArgs.Empty);targetTime.Minute += 1;break;}//display the time each secondif (updateTime.Second != second){Console.WriteLine("{0}:{1}:{2}",this.Hour.ToString(), this.Minute.ToString(), this.Second.ToString());}this.Hour = updateTime.Hour;this.Minute = updateTime.Minute;this.Second = updateTime.Second;}} 这篇关于代表和活动 - 麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 09:51