[Microsoft bot builder sdk] 如何在Microsoft bot應用程序中創建子線程或輔助線程,該應用程序可以不斷偵聽第三方Web APi併發布消息給不同連接器上的用戶。如何在Microsoft bot應用程序中創建子線程或工作線程
我創建了一個bot,並希望該bot能夠不斷收聽第三方Web API。這項工作應該在一個單獨的線程中完成。但是,如果我在Dialog類中創建線程,它不會向用戶發佈響應,因爲對話框對象在等待用戶輸入時被暫停。
[Serializable]
public class SampleDialog : IDialog<object>
{
protected int count = 1;
[NonSerialized]
Thread ChildTask;
public async Task StartAsync(IDialogContext context)
{
ChildTask = new Thread(new ParameterizedThreadStart(RunChildTask));
ChildTask.Start(context);
context.Wait(MessageReceivedAsync);
}
private void RunChildTask(object context)
{
IDialogContext contex = context as IDialogContext;
while (true)
{
this.count++;
contex.PostAsync($"{this.count++}: You said From child");
Thread.Sleep(1000);
}
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
if (message.Text == "reset")
{
PromptDialog.Confirm(
context,
AfterResetAsync,
"Are you sure you want to reset the count?",
"Didn't get that!",
promptStyle: PromptStyle.None);
}
else
{
await context.PostAsync($"{this.count++}: You said {message.Text}");
context.Wait(MessageReceivedAsync);
}
}
RunChildTask方法被調用但不回發給用戶。