3

I am trying to schedule a reboot programmablly on winxp by using c# currently, i program it to add a scheduled task in winxp to reboot in 5 min however, it does not always work because the machine time maybe changed after restart (there is time server in my environment). so that means the scheduled task time is not accurate, in turn my reboot does not work on expected time.

For example, when i added scheduled reboot task, the machine time is 11:05 am and the reboot the is scheduled to 11:10am. then i joined xp to domain and restart, the time got synced with time server and machine changed to 1:01pm. in this case, the scheduled task has a time 11:10am on same day, it's past. of course it won't work.

so anybody know any other way to do it?
ps: this program is used before joining xp to domain and reboot is scheduled because it's 2nd reboot and i need it automatically reboot without user interactions

ikel
  • 1,790
  • 6
  • 31
  • 61

2 Answers2

5

Use the built-in shutdown command by launching a new process from C#:

using System;
using System.Diagnostics;

namespace ShutdownApp
{
    class ShutdownApp
    {
        static void Main(string[] args)
        {
            Process shutDown = new Process();
            int shutdownTimeInSeconds = 600; // this will shutdown in 10 minutes, use DateTime and TimeSpan functions to calculate the exact time you need    
            shutDown.StartInfo.FileName  = "shutdown.exe";
            shutDown.StartInfo.Arguments = string.Format("-r -t {0}", shutdownTimeInSeconds);

            shutDown.Start();
        }
    }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Brian Deragon
  • 2,929
  • 24
  • 44
  • thanks, i got this part as well as how to add scheduled tasks in c#, it's just a problem with time i think – ikel Dec 01 '11 at 17:55
  • This method will work even if Task Scheduler is not on, or the time changes due to joining a domain, etc. – Brian Deragon Dec 01 '11 at 17:55
  • yes, this will reboot, but i need 2nd reboot in 5 mins without user interactions – ikel Dec 01 '11 at 17:57
  • Don't add it to scheduled tasks, calculate the time using datetime/timespan, find the days/hours/minutes portion till the time you want, calculate it, and run the process. When you're done, even if the time changes, the timer will still be running in the background. – Brian Deragon Dec 01 '11 at 17:58
  • So if you need a 2nd reboot in 5 minutes without user interactions, add it to scheduled task on "User Startup" (not time dependent) and give it a -t 300 (or 300 in my shutdownTimeInSeconds), and it will reboot 5 minutes after user startup, regardless if it joins the domain and syncs/changes time. – Brian Deragon Dec 01 '11 at 17:59
  • you mean i dont have to schedule a reboot task for 2nd reboot? – ikel Dec 01 '11 at 18:00
  • Not technically, if you wanted to, you could add your program to startup, and launch your program with an argument, that passes in the amount of seconds to the next reboot (300), so the first time they launch your program with no arguments it adds itself to startup, reboots, and the 2nd time, you launch it with that 300 second argument, it launches the command to reboot 5 minutes later, and pulls itself from startup. But it won't require any user interaction, correct. – Brian Deragon Dec 01 '11 at 18:04
  • which startup registry should i add command to?? the runonce key only executes when user logs in – ikel Dec 01 '11 at 18:43
  • The `Process` object here should be wrapped in a `using` statement – Liam Apr 10 '19 at 15:07
-1

You need to follow @JohnSaunders advice and force a time synchronization before you add the scheduled task. You could do this using NTP or SNTP. See this post.

Community
  • 1
  • 1
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • but it's for brand new machines, cant really do time sync before it's joined to domain, can we? – ikel Dec 01 '11 at 17:54
  • This works if you have authority to update the system clock - it doesn't matter whether you are joined to a domain or not. This is probably overkill though given @BrianDeragon solution. – SliverNinja - MSFT Dec 01 '11 at 17:57