S
Snowbleach
Hello, I am having trouble with the following console application. I
am using delegates/event to make a clock raise an alarm when a certain
amount of time has been reached. The following compiles, but code does
nothing else then to produce an endless loop. Any help would be
greatly 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;
else
return false;
}
public override string ToString()
{
return this.Hour.ToString() + ":" + this.Minute.ToString()
+ ":" + this.Second.ToString();
}
public CountdownClock(int theHour, int theMinute, int
theSecond)
{
hour = theHour;
minute = theMinute;
second = theSecond;
}
//default constructor - takes no action
public CountdownClock()
{}
//the delegate
public delegate void AlarmEventHandler(CountdownClock o,
EventArgs e);
//public instance of the delagate
public 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 += new
CountdownClock.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();
}
}
}
am using delegates/event to make a clock raise an alarm when a certain
amount of time has been reached. The following compiles, but code does
nothing else then to produce an endless loop. Any help would be
greatly 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;
else
return false;
}
public override string ToString()
{
return this.Hour.ToString() + ":" + this.Minute.ToString()
+ ":" + this.Second.ToString();
}
public CountdownClock(int theHour, int theMinute, int
theSecond)
{
hour = theHour;
minute = theMinute;
second = theSecond;
}
//default constructor - takes no action
public CountdownClock()
{}
//the delegate
public delegate void AlarmEventHandler(CountdownClock o,
EventArgs e);
//public instance of the delagate
public 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 += new
CountdownClock.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();
}
}
}