如何设置等待时间等待结果

时间:2019-01-01 09:50:06

标签: c# async-await botframework

我的ms bot框架中有:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
    var activity = await result as Activity;
    context.Wait(MessageReceivedAsync);
}

如何设置等待时间await result

1 个答案:

答案 0 :(得分:3)

我建议您尝试以下操作:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
    // Set the delay to whatever timeout interval you need (in milliseconds)
    if (Task.WaitAny(result.ToTask(), Task.Delay(1000)) == result) 
    {
        var activity = await result as Activity;
    }
    else
    {
        // Didn't complete during the defined interval
    }

    context.Wait(MessageReceivedAsync);
}

更新

添加了ToTask()用法,可将IAwaitable转换为Task可以使用的WhenAny

希望有帮助!