问题
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