0

My question will probably sound stupid, but please bear in mind I don't know much about programming.

I managed to make a simple program with a timer. When you run it, it waits for 8 hours and then runs an .exe file in the same folder and then it closes itself.

The code is:

namespace Timer
{
    public partial class Timer : Form
    {
        public Timer()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"Alarm.exe");
            System.Windows.Forms.Application.Exit();
        }
    }
}

I would like to change it so that when the timer runs out, it checks the time and only runs the .exe and closes itself if it's 8am or later. If it's not 8am yet, it should wait and run the .exe and close itself at 8am. Is there any easy way to accomplish this?

Thank you very much for your help.

Dunno123
  • 77
  • 5
  • 2
    What's the reason for not just using Windows Task Scheduler to schedule the second exe to run at 8am? – Damien_The_Unbeliever Feb 12 '21 at 13:42
  • @Damien_The_Unbeliever I think it's because it's not _exactly_ 8 AM. It's only 8 AM if the 8 hour waiting period ends before 8 AM. If it ends at for example 9 AM, it should fire immediately. – Fildor Feb 12 '21 at 13:44
  • It is not meant to run the second exe at 8am. It is meant to run the second exe after 8 hours of running the program, except if the timer runs out before 8am. As @Fildor said. – Dunno123 Feb 12 '21 at 13:47
  • You could check current Time against "Today, 8 AM" and if it's smaller set the timer again to fire after the difference from "now" to "8 AM". – Fildor Feb 12 '21 at 13:50
  • Just see whether the current time (`DateTime.Now.TimeOfDay`) is before/after the target time (`new TimeSpan(8, 0, 0)`). Alternatively check whether the current time's number of hours (`DateTime.Now.Hour`) is before/after the target number of hours (`8`) – canton7 Feb 12 '21 at 13:50
  • Voted to reopen. Disagreeing with the dupe. – Fildor Feb 12 '21 at 13:52
  • 2
    @Fildor Explaining *why* you disagree with the dup will help other decide whether they want to join your vote to reopen – canton7 Feb 12 '21 at 13:52
  • 1
    Are we assuming before/after are with respect to midnight or is this a Gremlins feeding problem? (I.e. define what period you consider to be "before 8am". Even then, you can compute when the period should end, adjust it as required and still just schedule the task. There's no need to have a program sitting perfectly still for 8 hours doing nothing. – Damien_The_Unbeliever Feb 12 '21 at 13:57
  • 1
    @canton7 Ok, I thought that was obvious after clarification from OP. I disagree, because this is _not_ about "daily, at a specific time". It somewhat overlaps, but in my book it is not a duplicate. – Fildor Feb 12 '21 at 14:00
  • @Damien_The_Unbeliever Before 8am = midnight to 8am I would say. I am going to try to add some other things later when I learn how to do it so it won't be sitting still. https://stackoverflow.com/questions/3243348/how-to-call-a-method-daily-at-specific-time-in-c doesn't answer my question. – Dunno123 Feb 12 '21 at 14:05
  • 1
    @Fildor Some of the answers contain the logic the OP needs, though. E.g. [this one](https://stackoverflow.com/a/64771833/1086121). OP's timer needs to be longer, but the logic about whether to trigger is similar, no? – canton7 Feb 12 '21 at 14:06
  • @canton7 I just tried what you suggested. Now if the timer runs out before 8am, it never runs the .exe though. – Dunno123 Feb 12 '21 at 14:25
  • @Dunno123 Right, so you'll need to figure out what you want to do in that case, and then write code to implement that. Perhaps you want to reschedule the timer to run slightly after 8am? – canton7 Feb 12 '21 at 14:28
  • @canton7 Well it should wait and run the .exe and close itself at 8am. I don't think I will be able to figure this out myself. I guess I will need another timer and perhaps check the time when I am starting the program and then have one timer that would be 8 hours and another that would be 24 hours minus current time + 8 hours, but that's just too complicated for me :/ – Dunno123 Feb 12 '21 at 14:35
  • Or just one timer, and you change the interval? You can see how long it is to 8am (in the same day) by doing something like `new TimeSpan(8, 0, 0) - DateTime.Now.TimeOfDay`. Also see the answers in the linked question. – canton7 Feb 12 '21 at 14:40
  • @canton7 Thank you, that makes sense, but I still need another timer for it I guess as this just calculates the time and doesn't add it? Either way I did something wrong and I am getting a whole bunch of errors now, but I will keep trying. – Dunno123 Feb 12 '21 at 15:28
  • You claim that your program waits 8 hours then runs the other program. But there's nothing in the code you posted that waits at all. Furthermore, why don't you just wait for a variable amount of time, computed from "the difference between 8am and now"? The originally used duplicate (which I'm using again) tells you _exactly_ how to do that. – Peter Duniho Feb 17 '21 at 03:44

0 Answers0