所以我运行某种类型的游戏,我想添加一个命令 !uptime 来显示服务器自上次打开以来运行了多长时间或其他
此代码(来自微软网站)显示了滴答计数并正确显示
int result = Environment.TickCount & Int32.MaxValue;
player.SendMessage("Result: " + result);
但我希望能够显示其运行时间以分钟为单位。
最佳答案
来自MSDN documentation ,我们可以看到 Environment.TickCount
Gets the number of milliseconds elapsed since the system started.
然后您可以将其转换为分钟,如下所示:
var minutes = (Environment.TickCount - serverStartTickCount) / 60000; // 1000 ms/s * 60 s/m
或者,您可能需要考虑在服务器启动时存储 DateTime.Now
。假设您的类名为 Program
,您可以将其添加到其中:
public static readonly DateTime ServerStartTime = DateTime.Now;
然后在运行命令时执行此操作:
var uptime = DateTime.Now - Program.ServerStartTime;
var minutes = uptime.TotalMinutes;
正如 @Carlos 指出的那样,当 Environment.TickCount
每隔几周滚动一次时,这将使您能够获得准确的正常运行时间。
关于c# - 查找程序的正常运行时间,但在几分钟内显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35210746/