0

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

Theraot
  • 31,890
  • 5
  • 57
  • 86
Maximus
  • 29
  • 2
  • 7
  • Just to check if I understood correctly. The only reason why you are checking if "now" is between the end and start is so that when the "Tick" hits between that time you want to execute that code once. Am I correct? – fsschmitt Oct 05 '16 at 19:35
  • Yeah, I couldn't work out any other way – Maximus Oct 05 '16 at 19:35
  • Use a library to handle time for you like [Quartz.NET](https://www.nuget.org/packages/Quartz), it makes stuff like alarms 100x easier. – Scott Chamberlain Oct 05 '16 at 19:36
  • 1
    Lots of other ways to do what you are doing, some probably better, but to answer your question: `MyTimer.Tick -= MyTimer_Tick;` removes the handler. – PeteGO Oct 05 '16 at 19:38
  • Or calculate the interval based on when you want it to activate (target time minus now). But still remove the handler if you want it to only execute once. – PeteGO Oct 05 '16 at 19:41
  • 1
    One more option would be to just do what you need to in this program and schedule the .exe using the Windows Task Scheduler. – PeteGO Oct 05 '16 at 19:42

2 Answers2

0

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.

fsschmitt
  • 599
  • 5
  • 10
0

You are using Process.Start to play the song, and leaving it there.

You could try using the return value of Process.Start to check if the process is still running, or even to kill it.

Besides, you are probably better served if you use a library to play your mp3. then you will have finer control over its play time.

Some options include:

  • WMPLib (for Windows, Microsoft Eula)
  • NAudio (for Windows, Ms-PL)
  • DirectShow (for Windows, LGPL)
  • irrKlang (for Windows, Linux and Mac, free for non-commercial use)
  • Alvas.Audio (for Windows, paid, privative)

Note: there is also OpenAL via OpenTK or Monogame but you probably don't wany any of the 3D stuff.

Community
  • 1
  • 1
Theraot
  • 31,890
  • 5
  • 57
  • 86