0

I want a task (.exe) to run everyday at 8:55 am which will span over 30 miniutue.so i created a console application which will start that task(.exe) by using process.start().I don't want a sheduler or windowservice rather i am happy with a console application.But the logic used in console application some times fails ie that is not invoking the task (.exe).I create infinite loop by using while(true). The code i am posting down(console code),how to correct it

while (true)
            {
                if (DateTime.Now.Hour == 8 & DateTime.Now.Minute == 55 && DateTime.Now.Second == 0)
                {
                    Process.Start(".exe");//not real code
                }
                else
                    Thread.Sleep(1000);
            }

EDIT I need to run the task only one time by using console application,not more than that in a day

peter
  • 8,158
  • 21
  • 66
  • 119
  • Possible duplicate of [How to call a method daily, at specific time, in C#?](http://stackoverflow.com/questions/3243348/how-to-call-a-method-daily-at-specific-time-in-c) – Nikola Jan 26 '16 at 17:44
  • You're missing the second mark. – Maritim Jan 26 '16 at 17:44
  • @Nikola it is not a duplicate , i have edited my question – peter Jan 26 '16 at 17:48
  • @peter, in the said question there are examples how to achieve this without using scheduler :) – Nikola Jan 26 '16 at 17:58
  • @Nikola that contains an approch,but see serhiyb answer down , very simple and contain less number of lines.But i have to verify it – peter Jan 26 '16 at 18:12

2 Answers2

2

Because your condition is missing seconds. Thread.Sleep(1000) won't guarantee it will be paused exactly when each second is 0, so you need to be smarter than that.

Ivan G.
  • 5,027
  • 2
  • 37
  • 65
  • How can i get rid of that? – peter Jan 26 '16 at 17:47
  • The first most obvious method - check that current time is equal or greater than 8:55 am, then start the process. – Ivan G. Jan 26 '16 at 17:48
  • @TobiasKnauss that might not work either, depending on how busy CPU is and how much attention your process will get. – Ivan G. Jan 26 '16 at 17:51
  • @aloneguid I need to run the task only one time by using console application,not more than that in a day – peter Jan 26 '16 at 17:52
  • 1
    You're right, but it will increase the chance a lot. He could additionaly skip the seconds check and set a flag to remember that it was run at that day. – Tobias Knauss Jan 26 '16 at 17:54
  • 1
    @peter you can remember the day the task was run in a variable and don't trigger again. If you want more flexibility check out http://www.quartz-scheduler.net/ i've used it many times for scheduling. – Ivan G. Jan 26 '16 at 17:55
1
        while (true)
        {
            var now = DateTime.Now;
            var schedule = new DateTime(now.Year, now.Month, now.Day, 8, 55, 00);
            if (schedule < now) schedule = schedule.AddDays(1);

            Thread.Sleep(schedule.Subtract(now));

            Process.Start(".exe");
        }
serhiyb
  • 4,753
  • 2
  • 15
  • 24