如何处理Botframework SDK3 C#中的超时 [英] How to deal with timeouts in botframework SDK3 C#
问题描述
很抱歉我是一个新手,我有很多问题.
I'm so sorry that I'm a newbie that I have so many questions.
我的机器人状态保存在数据库中.因此它永远不会超时.我使用团队. 但是,如果用户在10分钟内未回复,我想重新开始对话.
My Bot state is save in DB. So it would never timeout. And I use teams. But I want to restart conversation if user don't reply for 10 minutes.
这是我的解决方案. 根据此,我还制作了静态字典,可以帮助我处理每次对话.定时器很容易.并借助示例代码,我确实中断了堆栈.但是,一旦完成,它将回到原始对话.解决这个问题.我根据此
This is my solution. According this, I also made a static dictionary which could help me to handle every conversations' timer easily. And with the help of Sample Code, I did interrupt the stack. But once it's finished, it will be back to the original conversation. To solve this problem. I cleared the state according this
推荐答案
我确定有很多方法可以解决此问题,但这是您可以尝试的一种方法.
I'm sure there are a number of ways to solve this but here is one way you can try.
在您的Global.asax中
In your Global.asax
为您的线程定义此取消令牌来源
Define this cancellation token source for your thread
CancellationTokenSource _getTokenAsyncCancellation = new CancellationTokenSource();
在Application_Start()代码中,设置带有while循环的Task线程.看起来像这样:
In Application_Start() code setup a Task thread with a while loop. It will look something like this:
Task.Factory.StartNew(async () =>
{
while (!_getTokenAsyncCancellation.IsCancellationRequested)
{
try
{
//Check timespan between last message timestamp vs NOW
//if timespan > 9 minutes
// send message
}
catch (Exception ex)
{
Trace.TraceError(ex.Message);
}
await Task.Delay(TimeSpan.FromMinutes(1), _getTokenAsyncCancellation.Token).ConfigureAwait(false);
}
}).ConfigureAwait(false);
此外,添加Application_edn方法以彻底清除问题
Also, add an Application_edn method to sht things down cleanly
protected void Application_End()
{
_getTokenAsyncCancellation.Cancel();
}
现在,您还没有完成.您还需要一些跟踪用户的最后一条消息时间戳.基本上,每次自动程序收到用户的新消息时,您都会设置此时间戳,而while循环中的代码将对此进行检查.
Now, you're not quite done. You will also need somewhat to track the user's last message timestamp. Basically, each time the bot receives a new message from the user yo will set this timestamp and the code in your while loop will check against that.
我希望这会有所帮助,并为您提供一些尝试方法的想法.
I hope this helps and gives you some ideas on what to try.
这篇关于如何处理Botframework SDK3 C#中的超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!