Created
June 30, 2012 09:49
-
-
Save Cheesebaron/3023188 to your computer and use it in GitHub Desktop.
Sannes Brain is sleepy in Daylight
This file contains 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
using System; | |
using System.Threading; | |
namespace SannesBrain | |
{ | |
public class Brain | |
{ | |
public static void Main(string[] args) | |
{ | |
EnvironmentVariables.DaylightChanged += (sender, eventArgs) => | |
Console.WriteLine(!EnvironmentVariables.Daylight | |
? "Weee!! Awake! What to do?" | |
: "Sleepy... zzz..."); | |
ThreadPool.QueueUserWorkItem(e => | |
{ | |
while(true) | |
{ | |
var timeNow = DateTime.Now.TimeOfDay; | |
TimeSpan sleep; | |
if (timeNow < new TimeSpan(4, 41, 0) || timeNow > new TimeSpan(21, 56, 0)) | |
{ | |
EnvironmentVariables.Daylight = false; | |
sleep = new TimeSpan(4, 41, 0) - timeNow; | |
} | |
else | |
{ | |
EnvironmentVariables.Daylight = true; | |
sleep = new TimeSpan(21, 56, 0) - timeNow; | |
} | |
Thread.Sleep(sleep); | |
} | |
}); | |
Console.ReadKey(); | |
} | |
} | |
public delegate void EnvironmentChangedEvent(object sender, EventArgs e); | |
public static class EnvironmentVariables | |
{ | |
private static bool _daylight; | |
public static bool Daylight | |
{ | |
get { return _daylight; } | |
set | |
{ | |
if (_daylight == value) return; | |
_daylight = value; | |
if (null != DaylightChanged) | |
DaylightChanged(null, EventArgs.Empty); | |
} | |
} | |
public static event EnvironmentChangedEvent DaylightChanged; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment