11 posts

## Multiple Alaram Clock

Back to Forum: Coffeehouse
• Hi Can Anyone tell me how i can develop alarm clock in c# with multiple alarms. plzzz reply soon

• Explain your problem in detail please.

• @ZippyV: Lemme see if I can explain for the OP.

You see I'm a student that has an assignment due and rather than do the research I'd like you to do it for me -- for free. The assignment is due soon so I'd appreciate it if you tell me what to do really soon.

If we all believed in unicorns and fairies the world would be a better place.
• @DeathByVisualStudio: Yeah, I thought when I read the question I wish sometimes it was that simple to just ask someone else to do your work for you.

I can see it now.

Your manager comes in and says were are we on that new invoicing module.
You say: I am waiting for somebody on channel nine to give me the code to paste into visual studio.

• @DeathByVisualStudio: I'm trying to get him to explain his problem and divide a big problem into multiple smaller problems until every problem is small enough to be solved in a couple of lines of code.

• @ZippyV: But what if his statement is the small problem?

If we all believed in unicorns and fairies the world would be a better place.
• For him it's not.

• @ZippyV: And that's the part he's missing.

If we all believed in unicorns and fairies the world would be a better place.
• Basic solution for multiple alarms:  Have a single timer that fires at appropriate intervals (depending on the accuracy or your alarms that could be every second or every minute).  When the timer fires, check a list of alarm times to see if you have reached one yet; if so show the alarm message to the user.

Simple.

Herbie

• ```  // Known Bugs: It is theoretically possible to "skip" an alarm if within seconds of one another
// TODO: One thread per alarm; sub-second accuaracy, replace TimeSpan with maths
static DateTime? Watcher(DateTime[] alarms)
{
// Find the next nearest alarm.
long nextAlarm = -1;
foreach (DateTime alarm in alarms)
if (alarm.Ticks - DateTime.Now.Ticks > 0 && (alarm.Ticks < nextAlarm || nextAlarm == -1))
nextAlarm = alarm.Ticks;

// Below checks/sleeps are "optional." They just save it calling DateTime.Now every 1000 ms.
while (TimeSpan.FromTicks(nextAlarm - DateTime.Now.Ticks).Days > 0)

while (TimeSpan.FromTicks(nextAlarm - DateTime.Now.Ticks).Hours > 0)

while (TimeSpan.FromTicks(nextAlarm - DateTime.Now.Ticks).Minutes > 0)

// Do not remove the below. Is the actual production code within this function.
while (TimeSpan.FromTicks(nextAlarm - DateTime.Now.Ticks).Seconds > 0)

if (nextAlarm == -1)
return null;

Thread.Sleep(1000); // Stops re-trigging within the second. Technically might trigger after target second.
return new DateTime(nextAlarm); // Object reference to the original alarm would be cheaper here.
}

static void Main()
{
DateTime[] alarms = new DateTime[] { new DateTime(2012, 04, 06, 10, 01, 30), new DateTime(2012, 04, 06, 10, 02, 30) };
do
{
DateTime? trigger = Watcher(alarms);
if(trigger == null) // We're out of alarms, time to die
break;
Console.WriteLine(trigger.ToString());
} while (true);
}```

• , Dr Herbie wrote

Basic solution for multiple alarms:  Have a single timer that fires at appropriate intervals (depending on the accuracy or your alarms that could be every second or every minute).  When the timer fires, check a list of alarm times to see if you have reached one yet; if so show the alarm message to the user.

Simple.

Herbie

My basic solution would be to use the quartz scheduler. Written well (the .NET port), tried and tested so relatively bug free.

## Conversation locked

This conversation has been locked by the site admins. No new comments can be made.