本文介绍了正确使用Task.Delay的拖延关键presses的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code以下的工作不过我很好奇,如果我使用Task.Delay的()是不是最好的做法我在做什么。我需要一个延迟,以确保GameHandler有足够的时间来处理我的钥匙presses。不过,我不知道是否有在做这样的事情的更好方法。

 异步公共任务<布尔> CloseMenusAsync(令牌的CancellationToken)
            {
                而(GameHandler.Menu.IsOpen&安培;&安培;!token.IsCancellationRequested)
                {
                    如果(GameHandler.Menu.IsOpen&安培;&安培; GameHandler.Menu.DialogText.Question ==打开)
                    {
                        GameHandler.SendKey preSS(主要code.DownArrow);
                        等待Task.Delay(150);
                        GameHandler.SendKey preSS(主要code.EnterKey);
                        等待Task.Delay(150);
                    }
                    如果(GameHandler.Menu.IsOpen&安培;&安培; GameHandler.Menu.Selection ==交易)
                    {
                        GameHandler.SendKey preSS(主要code.EscapeKey);
                        等待Task.Delay(150);
                        GameHandler.SendKey preSS(主要code.EnterKey);
                        等待Task.Delay(150);
                    }
                    GameHandler.SendKey preSS(主要code.EscapeKey);
                    等待Task.Delay(150);
                }                返回GameHandler.Menu.IsOpen!;
            }


解决方案

既然你是在发送端,它的罚款。

另一种选择是使用,具有较高学习曲线。

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.

这篇关于正确使用Task.Delay的拖延关键presses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 08:16