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?