-2

I've got the code:

 public static Stopwatch stopWatch = new Stopwatch();

    private void start_Checker()
    {
        stopWatch.Start();
        while (true)
        {
            TimeSpan ts = stopWatch.Elapsed;
            if (ts.Minutes % 15 == 0)
            {
                Core.sendLog("Detected " + ts.Minutes + " of possible inactivity. Bot might be in game. Waiting " + (Core.inactivity_Max - ts.Minutes) + " minutes before restarting", false);
            }
            if (ts.Minutes >= Core.inactivity_Max)
            {
                Core.sendLog(Core.inactivity_Max + " minutes of inactivity - restarting the bot.");
                Thread.Sleep(500);
                Process.Start(Assembly.GetExecutingAssembly().Location);
                Environment.Exit(0);
            }
            Thread.Sleep(10000);
        }
    }

and this one in the Core class:

   public static void sendLog(string text, bool isAction = true)
    {
        if (isAction)
        {
            Listener.stopWatch.Reset();
        }
        using (WebClient client = new WebClient())
        {
            try
            {
                string log = "[" + account[0] + "] " + text + " | Time: " + DateTime.Now;
                client.OpenRead(url + @"elements/logs/logs.php?file=" + used_email + "&text=" + log);
            }
            catch (Exception)
            {
                return;
            }
        }
    }

It is supposed to send the log every 15 minutes, and if the ts.Minutes is longer than max inactivity time - it's supposed to reset the application. Everytime sendLog() is executed, it resets the stopwatch's time.

Current code results in the log file being spammed with messages like below:

[ChristianFromDK] Detected0 of possible inactivity. Bot might be in game. Waiting 80 minutes before restarting | Time: 7/21/2017 7:50:18 PM

What have I done wrong?

P Sawicki
  • 189
  • 1
  • 2
  • 9
  • 2
    For the first minute the minute value is zero and zero modulo 15 is zero. – Sami Kuhmonen Jul 21 '17 at 18:05
  • 2
    _"For the first minute the minute"_ -- and for the 15th minute, and the 30th minute, and so on. Your implementation is just plain a bad idea. If you want to do something every 15 minutes, then use a timer. Don't poll the clock. – Peter Duniho Jul 21 '17 at 18:06

1 Answers1

0

Since you are just checking for modulo there will be a message every ten seconds while the StopWatch is less than one minute and for every 15 minutes from there on. This is due to zero modulo 15 being zero so the condition matches.

If you want to call this only once every 15 minutes you will have to compare the current value to a previous value, if it's more than 15 minutes send the message and then set the previous value to current value. Then keep comparing.

This way it will happen only once when the timer gets to 15 minutes. Also remember to zero the previous value when the stopwatch is zeroed.

You could also use a timer for it canceling it when the stopwatch is zeroed. Usually system timers are less resource intensive.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • I'm really confused. Can't I just do if( ts.Minutes != 0) ? why is the result of ts.Minutes always 0 even if the time passed since last action is longer than 0 minutes? – P Sawicki Jul 21 '17 at 18:17
  • @PSawicki Because `Minutes` is the minute component. `TotalMinutes` would give you the total time in minutes as floating point number. But it will probably never be exactly zero, 15 etc so comparing for exact number won't work. – Sami Kuhmonen Jul 21 '17 at 18:19
  • I can just convert that to int, can't I? – P Sawicki Jul 21 '17 at 18:21
  • @PSawicki Then it's exactly the same as using `Minutes` until you go past one hour. It won't change anything. – Sami Kuhmonen Jul 21 '17 at 18:22