예제 #1
0
        private void SyncTime()
        {
            // Do not sync time if this is a MAC
            if (GetOperatingSystemType() == OperatingSystemType.Mac)
            {
                APPHelper.SetTimeMac();
                return;
            }

            int attempt = 0;

            ServiceController serviceController = new ServiceController("w32time");

            if (serviceController.Status != ServiceControllerStatus.Running)
            {
                serviceController.Start();
            }

            Log("w32time service is running");

            SendHeartBeat();

            while (attempt < 25)
            {
                try
                {
                    Process processTime = new Process();
                    processTime.StartInfo.FileName               = "C:\\Windows\\System32\\w32tm.exe";
                    processTime.StartInfo.Arguments              = "/resync /force";
                    processTime.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                    processTime.StartInfo.UseShellExecute        = false;
                    processTime.StartInfo.RedirectStandardOutput = true;
                    processTime.Start();
                    string output = processTime.StandardOutput.ReadToEnd();
                    processTime.WaitForExit();

                    Log("Synced time with server: " + output);
                    SendLogMessage("MSG: " + output);

                    if (output.ToLower().Contains("the command completed successfully.") == true)
                    {
                        // Due to the possible time change, the timer may be out of sync, so start a new one.
                        StartHeartbeatTimer();
                        return;
                    }

                    Log("Retrying time sync in 10 sec.");
                }
                catch (Exception ex)
                {
                    Log("Unable to sync time: " + ex);
                }

                Thread.Sleep(10000);
                attempt++;

                SendHeartBeat();
            }

            throw new Exception("MSG: Could not sync time.");
        }