Created
December 17, 2015 11:02
-
-
Save chtenb/36bb3a2d2ac7dd511b96 to your computer and use it in GitHub Desktop.
Events are assignable, like delegates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AlarmNamespace | |
{ | |
public class AlarmEventArgs : EventArgs | |
{ | |
private DateTime alarmTime; | |
private bool snoozeOn = true; | |
public AlarmEventArgs (DateTime time) | |
{ | |
this.alarmTime = time; | |
} | |
public DateTime Time | |
{ | |
get { return this.alarmTime; } | |
} | |
public bool Snooze | |
{ | |
get { return this.snoozeOn; } | |
set { this.snoozeOn = value; } | |
} | |
} | |
public class Alarm | |
{ | |
private DateTime alarmTime; | |
private int interval = 10; | |
public delegate void AlarmEventHandler (object sender, AlarmEventArgs e); | |
public event AlarmEventHandler AlarmEvent; | |
public Alarm (DateTime time) | |
: this (time, 10) | |
{ | |
} | |
public Alarm (DateTime time, int interval) | |
{ | |
this.alarmTime = time; | |
this.interval = interval; | |
} | |
public void Set () | |
{ | |
while (true) | |
{ | |
System.Threading.Thread.Sleep (100); | |
DateTime currentTime = DateTime.Now; | |
// Test whether it is time for the alarm to go off. | |
if (currentTime.Hour == alarmTime.Hour && | |
currentTime.Minute == alarmTime.Minute) | |
{ | |
AlarmEventArgs args = new AlarmEventArgs (currentTime); | |
OnAlarmEvent (args); | |
if (!args.Snooze) | |
return; | |
else | |
this.alarmTime = this.alarmTime.AddMinutes (this.interval); | |
} | |
} | |
} | |
protected void OnAlarmEvent (AlarmEventArgs e) | |
{ | |
AlarmEvent (this, e); | |
} | |
static void Main () | |
{ | |
Alarm a = new Alarm (DateTime.Now.AddSeconds(1)); | |
a.AlarmEvent += (o, e) => Console.WriteLine (1); | |
a.AlarmEvent += (o, e) => Console.WriteLine (2); | |
a.AlarmEvent = (o, e) => Console.WriteLine (3); | |
a.Set (); | |
Console.Read (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mpipalia !
So true, So great.
thanks for the help