Click here to Skip to main content
15,071,262 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am a complete beginner. I am trying to find out how to make a program that has an 8 hour timer and when the time elapses it will run an .exe and close itself.

I have managed to do this, but I need to change it so that it won't run the .exe and close itself before 8am. It's a sort of alarm that should wake you up after 8 hours of running the program, but never before 8am.

I start the program at 1am. At 9am the timer elapses, runs Alarm.exe and closes itself.
I start the program at 12am. At 8am the timer elapses, runs Alarm.exe and closes itself.
I start the program at 11pm. At 7am the timer elapses, but I still want the program to run Alarm.exe and close itself at 8am.

Thank you very much for your help.

What I have tried:

This is my code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Timer
    public partial class Timer : Form
        public Timer()
        TimeSpan eight = new TimeSpan(8, 0, 0);

        private void timer1_Tick(object sender, EventArgs e)     
            TimeSpan now = DateTime.Now.TimeOfDay;
            if (now >= eight)
            // ? ? ? ? ? ? ? ? ? ?
Updated 13-Feb-21 7:53am

Why do you want to stop your Application ? It could work all the time ...
I would not work with a TimeSpan - I would work with the Timer inside your Application. Inside the Timer-Method you check if the actual Time is greater than for example 8:00 am. Now you start your Alarm and set a Bit to true which says that this Alarm was worked. If the Time is less tha 8:00 am then you reset this Bit. The 8:00 am Alarm is only startet if the Time is greater and the Bit is False ...

The same for each other time you want to do an action ...
Dunno123 13-Feb-21 8:56am
Thank you for your reply. I don't want to program to run all the time. I want to start the timer by starting the program. I am not sure I understand the solution you suggested :/
Ralf Meier 13-Feb-21 9:29am
It was only a Suggestion ...
But independant :
You should compare if DateTime.Now is greater than your preset-time. Therefore you should compare DateTime with DateTime - you could also set your preset-time as DateTime ...
So make your Variable eight to a DateTime and set it like this - then the compare will work ...
Dunno123 13-Feb-21 9:48am
The compare does work the way it is though.
Ralf Meier 13-Feb-21 11:18am
And your problem is ?
I suppose your Application exits without starting the Alarm - right ?
Dunno123 13-Feb-21 16:52pm
No, the alarm.exe starts fine and the application exits fine too. I just need to change it so that it won't run the .exe and close itself before 8am. That's all I am trying to achieve.
Ralf Meier 13-Feb-21 17:13pm
OK ... I think now I understand ...
When you start your Application for example a 10 am it also executes the alarm and stop itself because 10 am is after 8 am - am I right ?
If Yes you have to check when starting your application if your current time is before the alarm-time or after. If it is after you set a flag which prevents the execution of alarm inside Timer1_Tick. But inside the Method Timer1_Tick you reset the bit if the current time is before alarm-time.
Thats all ... ;-)
Dunno123 14-Feb-21 8:55am
No, that bit also works fine. The application checks DateTime.Now.TimeOfDay when the timer tick happens and then runs the .exe, which is always after 8 hours, so it doesn't matter when I start the application. Anyway thank you for your effort, I have already found the solution.
Here is a good comparison of available Timers by Jon Skeet: Timers[^]
He recommends the System.Threading.Timer and I have to agree with him, using the System.Windows.Forms.Timer can sometimes lead to problems.

You might also be interested in this tool: Hangfire[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900