How to run multiple functions at once in VB Console
Hey guys,
Our Computing teacher is encouraging us to try making games with Console in VB, and I like the idea, but there's one big problem with a lot of games that I don't know how I'd get around: there needs to be a game tick that's running constantly, but while the tick is running you would be able to use inputs as well. For example, say Tetris. The blocks are falling constantly, every tick they move down, but while the time is elapsing from one game-tick to the next, the Console needs to allow you to be able to read a ConsoleKey so you can change the path of that block or rotate it.
I've heard something about Threading which runs multiple functions at once - is this the sort of thing I need to be looking into? At the moment, all I know how to use is Threading.Thread.Sleep(Val in Milliseconds) to hold the Console, but is there any way to use this principle to accept input while the Console is waiting? Is there an even simpler way I'm overlooking?
If it is Threading or something even more complicated, I'd very much appreciate if someone could intuitively explain to me how it would work - I've only been doing VB for four months and a lot of the more complicated stuff I find very hard to grasp.
Hope you can help! Thanks a lot in advance!
Re: How to run multiple functions at once in VB Console
Yes you would use multi-threading. There are many, many different ways to implement it, depending on the circumstances. Given that you're talking about a "game tick" in this instance, I would suggest using a System.Timers.Timer object. It has a Tick event that you can handle to do something at regular intervals. The event is raised on a secondary thread by default so your event handler will be executed on a secondary thread, thus not interfering with your main thread that is processing user input and feedback.
Multi-threading does throw up a number of challenges though. Firstly, it can make debugging interesting, because the order of execution will often be different even if you do everything exactly the same way multiple times. Also, you have to be very careful when multiple threads are sharing resources. That can lead to some issues (race conditions, deadlocks) that can be hard to diagnose.