正确使用Task.Delay来延迟按键的时间[英] Proper use of Task.Delay to delay key presses

本文是小编为大家收集整理的关于正确使用Task.Delay来延迟按键的时间的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

下面的代码适用于我正在做的事情,但是如果我对task.delay()()的使用,我很好奇.我需要延迟以确保GameHandler有足够的时间处理我的钥匙按下.但是,我想知道是否有更好的方法来做这样的事情.

async public Task<bool> CloseMenusAsync(CancellationToken token)
            {
                while (GameHandler.Menu.IsOpen && !token.IsCancellationRequested)
                {
                    if (GameHandler.Menu.IsOpen && GameHandler.Menu.DialogText.Question == "Open")
                    {
                        GameHandler.SendKeyPress(KeyCode.DownArrow);
                        await Task.Delay(150);
                        GameHandler.SendKeyPress(KeyCode.EnterKey);
                        await Task.Delay(150);
                    }
                    if (GameHandler.Menu.IsOpen && GameHandler.Menu.Selection == "Trade")
                    {
                        GameHandler.SendKeyPress(KeyCode.EscapeKey);
                        await Task.Delay(150);
                        GameHandler.SendKeyPress(KeyCode.EnterKey);
                        await Task.Delay(150);
                    }
                    GameHandler.SendKeyPress(KeyCode.EscapeKey);
                    await Task.Delay(150);
                }

                return !GameHandler.Menu.IsOpen;
            }

推荐答案

由于您在发送方面,这很好.

另一个选项是使用 rx 学习曲线.

本文地址:https://www.itbaoku.cn/post/303375.html

问题描述

The code below works for what i'm doing however I'm curious if my use of Task.Delay() is not best practice. I need a delay to ensure that the GameHandler has enough time to process my Key presses. However, i'm wondering if there is a better approach to doing something like this.

async public Task<bool> CloseMenusAsync(CancellationToken token)
            {
                while (GameHandler.Menu.IsOpen && !token.IsCancellationRequested)
                {
                    if (GameHandler.Menu.IsOpen && GameHandler.Menu.DialogText.Question == "Open")
                    {
                        GameHandler.SendKeyPress(KeyCode.DownArrow);
                        await Task.Delay(150);
                        GameHandler.SendKeyPress(KeyCode.EnterKey);
                        await Task.Delay(150);
                    }
                    if (GameHandler.Menu.IsOpen && GameHandler.Menu.Selection == "Trade")
                    {
                        GameHandler.SendKeyPress(KeyCode.EscapeKey);
                        await Task.Delay(150);
                        GameHandler.SendKeyPress(KeyCode.EnterKey);
                        await Task.Delay(150);
                    }
                    GameHandler.SendKeyPress(KeyCode.EscapeKey);
                    await Task.Delay(150);
                }

                return !GameHandler.Menu.IsOpen;
            }

推荐答案

Since you're on the sending side, it's fine.

The other option is to use Rx, which has a higher learning curve.