0

I have a game loop which essentially is just:

While running
    game logic
End While

I have a stop button which when pressed sets running to False, however it cannot be pressed as there is an infinite loop happening, and the program cannot be exited with the exit button, but needs to be stopped in Visual Basic.

What's a better way to handle this so other things can still be done while this loop is happening?

werner_b
  • 47
  • 2
  • 11
  • 1
    In VBA doevents allows button presses to fall through, idk if VB.net has an equivalent. Edit it does, but apparently it's [not usually the best](https://stackoverflow.com/q/820471/6609896) – Greedo Mar 10 '19 at 09:42
  • @Greedo how would I implement that? – werner_b Mar 10 '19 at 09:43
  • 1
    @Greedo nevermind, got it. I just had to write 'Application.DoEvents()' in the while loop. cheers! – werner_b Mar 10 '19 at 09:47
  • Just put `Application.DoEvents()` in your while loop, something like [this](https://www.oreilly.com/library/view/vbnet-language-in/0596003080/re11.html) – Greedo Mar 10 '19 at 09:47
  • 3
    In VB.NET `DoEvents()` is in most cases a _**very bad**_ solution for multiple reasons - some of the most typical ones are mentioned in this blog post: [Keeping your UI Responsive and the Dangers of Application.DoEvents](https://blogs.msdn.microsoft.com/jfoscoding/2005/08/06/keeping-your-ui-responsive-and-the-dangers-of-application-doevents/). You should never utilize `DoEvents()` unless you know **exactly** what you're doing. If your loop causes the UI to freeze then you either need to rethink your logic or implement multithreading/task-based asynchronous programming. – Visual Vincent Mar 10 '19 at 10:40
  • A game loop like that doesn't work in an event-driven world like a GUI. You need to completely change your mindset of how you program, converting each game action into an event and handling it accordingly. Everything becomes transaction based, giving plenty of time for the UI to update itself and/or respond to other events. – Cody Gray - on strike Mar 10 '19 at 10:56

2 Answers2

0

you can take you game logic out of that infinite loop. let the user control your game with buttons and implement your game logic with those buttons.

e.g. if you want to restart the game, let the user click a restart button which restarts the game once.

0

Your best option would be to implement miltithreading. That way you can have your game logic and the associated whilt look on one thread, and allow your other thread to monitor user input.