暂停for循环,直到调用了外部回调方法

时间:2019-06-12 16:30:52

标签: c# multithreading for-loop telegram subscription

关于我要做什么的非技术性解释:

我有一个向用户发送图片的机器人,当用户单击“完成”按钮时,它将向他发送下一张图片,依此类推,直到没有其他图片为止。

问题:

使用我正在使用的API来监听该按钮的唯一方法是使用以下方法:

private static async void BotOnCallbackQueryReceived(object sender, CallbackQueryEventArgs callbackQueryEventArgs) {
        var callbackQuery = callbackQueryEventArgs.CallbackQuery;
        try {
            await Task.Run(() => {
                Bot.AnswerCallbackQueryAsync(callbackQuery.Id, "Answer has been submitted");
                string data = callbackQuery.Data.ToLower();
                if (data.Equals("notfound")){
                    //TODO He couldn't find the item
                }
                else if (data.Equals("done")) {

                }
            });
        }
    }

当然,可以通过以下方式订阅它:

Bot.OnCallbackQuery += BotOnCallbackQueryReceived;

要发送图像和菜单,我有一个for循环,该循环涉及List<Item>Item是POCO)。

await Task.Run(() => {
            // - Which buttons to send. MenuItem's constructor is (ButtonText, Callbackdata)
            List<MenuItem> menuItems = new List<MenuItem>();
            menuItems.Add(new MenuItem("Done", "done"));
            menuItems.Add(new MenuItem("Not found", "notFound"));
            //telegramLineItemObjects is a List<Item>
            foreach (Item item in telegramLineItemObjects) {
                string message = $"<b>Shelf Number:</b> {item.ShelfNumber}\n<b>Sku:</b> {item.Sku}\n<b>Barcode:</b> {item.Barcode}\n<b>Description:</b> {item.Description}\n<b>Quantity:</b> {item.Quantity}\n";
                //sendImage(Image URL, message, message parse mode)
                telegram.sendImage(item.Picture, message, ParseMode.HTML);
                Message sentMenu = telegram.sendMenu(message, menuItems);
            }
        }

我想暂停for循环,等待BotOnCallBackQueryReceived被调用,然后恢复for循环。

为此,我需要一种方法来侦听BotOnCallBackQueryReceived的调用时间。我该怎么做?

0 个答案:

没有答案
相关问题