Search code examples
c#windows-phonealarm

Windows Phone: Alarm class not firing as intended


Hi I'm creating a simple alarm clock for windows mobile and my alarm class is not firing as intended. Here is my code:

    public void AddAlaram()
    {
        Alarm alarm = new Alarm("Wakeup");
        alarm.Content = ("Time to wake up!");
        alarm.BeginTime = DateTime.Now.AddSeconds(5);
        alarm.ExpirationTime = DateTime.Now.AddHours(alarmHours);
        alarm.Sound= new Uri("/Resources/GoodMorning.wav",UriKind.Relative);
        ScheduledActionService.Add(alarm);
    }

I'm kinda new to programming in general. My layout is I made a listbox which has scrollable items from 1 - 24. I then set my alarm so that

    int alarmHours = 0;

        if(_1.IsSelected)
        {
            alarmHours = 1;
        }

and so on and so forth (long and unnecessary I know but I don't know how to optimize that atm).

So my understanding is that if for example 6 is selected then alarm expiration time should be the time now + 6 hours. but when I run the emulator it fires of in a few seconds.

Oh and here is the next line of the code it's a timer for the popup I made that appears when you click the add alarm button. I feel this is the one messing it up but I can't quite get how?

        AlarmPopup.IsOpen = true;

        System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();

        timer.Interval = new TimeSpan(0, 0, 0, 3, 0);

        timer.Tick += tick;

        timer.Start();

Any help would be appreciated. Thanks!


Solution

  • You have set the alarm to go off after 5 seconds:

    alarm.BeginTime = DateTime.Now.AddSeconds(5);
    

    The BeginTime is when the alarm fires. The ExpirationTime is when the alarm expires. This is useful for recurring alarms, e.g. if I want an alarm to go off every day but only for the next 5 days, then I should set the ExpirationTime to 5 days from now.