justnbusiness

Friday, July 27, 2007

Ghetto Alarm

Last night I found myself without a cellphone or an alarm clock, I needed to get up by 8am so I had to come up with something. Of course, being the nerd that I am, I decided to write up a quick alarm clock application before I fell asleep. I just plugged in my laptop and changed my power settings so that it would never go into sleep mode and executed the following program:


using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;

namespace GhettoAlarm
{
class Program
{
[DllImport("Kernel32.dll")]
public static extern bool Beep(UInt32 frequency, UInt32 duration);

static void Main(string[] args)
{
DateTime alarm = new DateTime(2007, 7, 27, 8, 0, 0);
while (true)
{
Console.Write("\rTime: " + DateTime.Now.ToShortTimeString());
Thread.Sleep(500);
if (DateTime.Now > alarm)
{
Beep(500, 250);
}
}
}
}
}



Needless to say it worked perfect! I'm not sure what would have happened if the laptop power mode was set to fall asleep but I think it probably would still work.

I think the interesting thing here though is how easy it is to just write up a little something to do what you want in C#. I know there is some talk going around about the new dynamic languages and how they are great for rapid prototyping but I just want to say with C# you can change which line of code is executing on the fly, change it and recompile on the fly and even change values in memory on the fly. I'm pretty happy with the rapid prototyping capabilities of C# and I'm a little skeptical of the value of dynamic languages in general... One of these days I'll have to try python again before being too certain about this though.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home