0
       private void WorkerThread()
    {
        AppConsole.Log("Restarter Worker Thread Started", ConsoleColor.Green);
        DateTime nextRestart = GetRestartTime(); //--- Fetch next restart
        AppConsole.Log(String.Format("Selected next restart date: {0}", nextRestart.ToString("F")), ConsoleColor.Green);
        _workerRunning = true;

        while (_workerRunning) {
            _waitHandle.WaitOne(1000);
            TimeSpan timeLeft = nextRestart.Subtract(DateTime.Now);
            SendMessage(String.Format("Time until next restart: {0} hours, {1} minutes, {2} seconds.",
                timeLeft.Hours, timeLeft.Minutes, timeLeft.Seconds));
            if (timeLeft.CompareTo(TimeSpan.Zero) < 0)
                DoRestart();
        }

Basically from this thread I need to output a message every 5 minutes if the server is more than 10 minutes from restarting and output a message every 2.5 minutes if the server is under 10 minutes from starting. I'm just wondering what would be the best way to do this.

Cheers

Matt
  • 1
  • 1

1 Answers1

0

You don't need a thread for that, you need a timer.

    DateTime nextRestart = GetRestartTime();
    AppConsole.Log("Restarter timer started", ConsoleColor.Green);
    Timer t = new Timer();
    t.Tick += (o, e) =>
    {
        TimeSpan timeLeft = nextRestart.Subtract(DateTime.Now);

        if (timeLeft.CompareTo(TimeSpan.Zero) < 0)
        {
            t.Stop();
            DoRestart();
        }
        else
        {
            SendMessage(String.Format("Time until next restart: {0} hours, {1} minutes, {2} seconds.",
                timeLeft.Hours, timeLeft.Minutes, timeLeft.Seconds));
            if (timeLeft.CompareTo(TimeSpan.FromMinutes(5)) < 0)
                t.Interval = 150 * 60 * 1000; // ms
            else if (timeLeft.CompareTo(TimeSpan.FromMinutes(10)) < 0)
                t.Interval = 300 * 60 * 1000; // ms
        }
    };
    t.Interval = (nextRestart - DateTime.Now).Milliseconds;
    t.Start();

All the code you need in one tidy package. You could even wrap it in a class.

shipr
  • 2,809
  • 1
  • 24
  • 32
  • Your answer would be more complete if you addressed the quantization aspect. If the process is started with 16 minutes to restart, does the OP want alerts at 11:00, 6:00, 3:30, and 1:00 minutes left, or at 15:00, 10:00, 7:30, 5:00, and 2:30 minutes left? Also, consider if there are 10:01 minutes left. Your code will set the next alert at 5:01 instead of 7:51, so it needs some adjustment. – ErikE Aug 14 '15 at 22:17
  • Right, the code is not perfect. For example, the original interval needs to be reduced by the warning time; as it is, it won't work. But it does answer the question of the best way to solve the problem. @ErikE As for the quantization, if the original time is set to expire 1ms too late, there will be no issue with comparisons. As for the time, the intervals will be as expected, because they are computed from the reset time, not the current time. – shipr Aug 14 '15 at 22:25