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

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.

Example:
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()
        {
            InitializeComponent();
        }
        TimeSpan eight = new TimeSpan(8, 0, 0);

        private void timer1_Tick(object sender, EventArgs e)     
        {
            TimeSpan now = DateTime.Now.TimeOfDay;
            if (now >= eight)
            {
            System.Diagnostics.Process.Start(@"Alarm.exe");
            System.Windows.Forms.Application.Exit();
            }
            else
            {
            // ? ? ? ? ? ? ? ? ? ?
            System.Diagnostics.Process.Start(@"Alarm.exe");
            System.Windows.Forms.Application.Exit();
            }
        }
Posted
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 ...
 
Share this answer
 
v2
Comments
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.
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[^]
 
Share this answer
 
v2

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



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