Coffeehouse Thread

11 posts

Multiple Alaram Clock

Back to Forum: Coffeehouse
  • User profile image
    enigma34

    Hi Can Anyone tell me how i can develop alarm clock in c# with multiple alarms. plzzz reply soon 

  • User profile image
    ZippyV

    Explain your problem in detail please.

  • User profile image
    DeathBy​VisualStudio

    @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.
    Last modified
  • User profile image
    devSpeed

    @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.

  • User profile image
    ZippyV

    @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.

  • User profile image
    DeathBy​VisualStudio

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

    If we all believed in unicorns and fairies the world would be a better place.
    Last modified
  • User profile image
    ZippyV

    For him it's not.

  • User profile image
    DeathBy​VisualStudio

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

    If we all believed in unicorns and fairies the world would be a better place.
    Last modified
  • User profile image
    Dr Herbie

    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

  • User profile image
    ManipUni

      // 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)
                    Thread.Sleep(86400000);
    
                while (TimeSpan.FromTicks(nextAlarm - DateTime.Now.Ticks).Hours > 0)
                    Thread.Sleep(3600000);
    
                while (TimeSpan.FromTicks(nextAlarm - DateTime.Now.Ticks).Minutes > 0)
                    Thread.Sleep(60000);
    
                // Do not remove the below. Is the actual production code within this function. 
                while (TimeSpan.FromTicks(nextAlarm - DateTime.Now.Ticks).Seconds > 0)
                    Thread.Sleep(1000);
    
                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); 
            }

  • User profile image
    vesuvius

    , 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.

     

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.