Proper use of Task.Delay to delay key presses

老子叫甜甜 提交于 2019-12-24 19:33:01

问题


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;
            }

回答1:


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

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



来源:https://stackoverflow.com/questions/11572559/proper-use-of-task-delay-to-delay-key-presses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!