Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created June 30, 2012 09:49
Show Gist options
  • Save Cheesebaron/3023188 to your computer and use it in GitHub Desktop.
Save Cheesebaron/3023188 to your computer and use it in GitHub Desktop.
Sannes Brain is sleepy in Daylight
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