0

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;
            }
poco
  • 2,935
  • 6
  • 37
  • 54

1 Answers1

0

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

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

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810