Maximus Maximus - 1 year ago 143
C# Question

Alarm Clock in C#

I had an idea to build an alarm clock in c# with the following features


  1. Show a to-do list for the day

  2. Play a song from a list of songs chosen



Completed features I've already done


  1. Show list of to-do's

  2. Play a song

  3. Show form between a certain time

    private void Form1_Load(object sender, EventArgs e)
    {
    Timer MyTimer = new Timer();
    MyTimer.Interval = (1 * 60 * 1000); // 1 mins
    MyTimer.Tick += new EventHandler(MyTimer_Tick);
    MyTimer.Start();
    }

    private void MyTimer_Tick(object sender, EventArgs e)
    {

    WakeUpProcess();
    }

    private void WakeUpProcess()
    {
    StreamReader CurrentAgenda = new StreamReader("C:/Users/Max/documents/visual studio 2015/Projects/Advanced_AlarmClock/Advanced_AlarmClock/File Resources/Todays Agenda.txt");
    TimeSpan start = new TimeSpan(06, 50, 0); //10 o'clock
    TimeSpan end = new TimeSpan(07, 00, 0); //12 o'clock
    TimeSpan now = DateTime.Now.TimeOfDay;
    System.IO.StreamReader CurrentAgendaRaw = new System.IO.StreamReader("C:/Users/Max/documents/visual studio 2015/Projects/Advanced_AlarmClock/Advanced_AlarmClock/File Resources/Todays Agenda.txt");
    string CurrentAgendaTxt = CurrentAgendaRaw.ReadToEnd();

    if ((now > start) && (now < end))
    {
    this.WindowState = FormWindowState.Normal;
    Output.Text = ("Good Morning Max" + Environment.NewLine + CurrentAgendaTxt);
    Process.Start("C:/Users/Max/Downloads/Juice Newton - Angel Of The Morning.mp3");

    }

    }



However, I need some solutions to problems I'm facing.
- Every time the time requirement is met (Between 6:50 and 7:00) a new instance is opened of the song. I only want one instance open.
- Also could someone helep me with how I'd get the program to wake up the computer if possible.

Thank you, Maximus

Answer Source

As a quick solution you can keep on a bool if the alarm has been triggered before or not.

bool alarmTriggered = false;
private void WakeUpProcess()
{
    StreamReader CurrentAgenda = new StreamReader("C:/Users/Max/documents/visual studio 2015/Projects/Advanced_AlarmClock/Advanced_AlarmClock/File Resources/Todays Agenda.txt");
    TimeSpan start = new TimeSpan(06, 50, 0); //10 o'clock
    TimeSpan end = new TimeSpan(07, 00, 0); //12 o'clock
    TimeSpan now = DateTime.Now.TimeOfDay;
    System.IO.StreamReader CurrentAgendaRaw = new System.IO.StreamReader("C:/Users/Max/documents/visual studio 2015/Projects/Advanced_AlarmClock/Advanced_AlarmClock/File Resources/Todays Agenda.txt");
    string CurrentAgendaTxt = CurrentAgendaRaw.ReadToEnd();

    if ((now > start) && (now < end) && !alarmTriggered)
    {
        alarmTriggered = true;
        this.WindowState = FormWindowState.Normal;
        Output.Text = ("Good Morning Max" + Environment.NewLine + CurrentAgendaTxt);            
        Process.Start("C:/Users/Max/Downloads/Juice Newton - Angel Of The Morning.mp3");

        }
    }
    else {
        alarmTriggered = false;
    }

}

PS: The libraries proposed in the comment for your question are a better way to go if you want to progress with your application.

PS2: I am assuming you are just trying stuff around and that this app is supposed to be running multiple days, otherwise removing the event listener is the best choice by far.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download