【问题标题】:Using stopwatch and it doesn't work properly使用秒表无法正常工作
【发布时间】: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();

标签: c# stopwatch discord


【解决方案1】:

尝试在异步函数中创建计时器。

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 2016-12-01
    • 2021-08-10
    • 2016-10-28
    • 1970-01-01
    相关资源
    最近更新 更多