ikel
ikel

Reputation: 1978

what is best way to schedule a reboot?

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

Upvotes: 3

Views: 1143

Answers (2)

Brian Deragon
Brian Deragon

Reputation: 2967

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();
        }
    }
}

Upvotes: 5

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31651

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.

Upvotes: -1

Related Questions