【发布时间】:2017-12-27 21:04:05
【问题描述】:
我有代码:
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);
}
}
核心类中的这个:
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;
}
}
}
它应该每 15 分钟发送一次日志,如果 ts.Minutes 长于最大不活动时间 - 它应该重置应用程序。
每次执行sendLog(),都会重置秒表的时间。
当前代码导致日志文件中包含如下消息:
[ChristianFromDK] Detected0 of possible inactivity. Bot might be in game. Waiting 80 minutes before restarting | Time: 7/21/2017 7:50:18 PM
我做错了什么?
【问题讨论】:
-
第一分钟分钟值为零,零模 15 为零。
-
“第一分钟” -- 以及第 15 分钟和第 30 分钟,以此类推。您的实施只是一个坏主意。如果你想每 15 分钟做一次,那就用计时器。不要轮询时钟。
标签: c# time timespan stopwatch