【发布时间】:2017-12-20 13:35:18
【问题描述】:
我正在使用 discord bot,我正在尝试检查时间服务的时间,但它只显示一次时间,然后写入 0
public async Task HandleCommand(SocketMessage messageParam)
{
// Don't process the command if it was a System Message
var message = messageParam as SocketUserMessage;
if (message == null || message.Author.IsBot)
return;
Console.WriteLine(message.Author);
time.Start();
// Create a number to track where the prefix ends and the command begins
int argPos = 0;
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
if (!(message.HasCharPrefix('>', ref argPos)))
return;
// Create a Command Context
var context = new CommandContext(dbot, message);
Last = message.Author;
// Execute the command. (result does not indicate a return value,
// rather an object stating if the command executed successfully)
var result = await commands.ExecuteAsync(context, argPos, services);
time.Stop();
time.Reset();
}
它调用了这个函数
[Command("ping"), Summary("Echos a message.")]
public async Task Say()
{
// ReplyAsync is a method on ModuleBase
await ReplyAsync("PONG " + CommandHandler.time.ElapsedMilliseconds);
}
【问题讨论】:
-
您在启动新线程后直接重置秒表。这意味着如果其他线程检查该值,则秒表已被重置。
-
您在重置计时器后是否缺少
time.Start();?