I understand that this is an old thread, but I’d like to provide two alternatives to the techniques suggested above. Before I go into them, here are some of the pitfalls with the proposals made so far:
PeekMessagecarries considerable overhead, as do the library methods that call it (SlimDXIsApplicationIdle).If you want to employ buffered RawInput, you’ll need to poll the message pump with
PeekMessageon another thread other than the UI thread, so you don’t want to be calling it twice.Application.DoEventsis not designed to be called in a tight loop, GC problems will quickly emerge.When using
Application.IdleorPeekMessage, because you’re only doing work when Idle, your game or application will not run when moving or resizing your window, without code smells.
To work around these (except 2 if you’re going down the RawInput road) you can either:
Create a
Threading.Threadand run your game loop there.Create a
Threading.Tasks.Taskwith theIsLongRunningflag, and run it there. Microsoft recommends that Tasks be used in place of Threads these days and it’s not hard to see why.
Both of these techniques isolate your graphics API from the UI thread and message pump, as is the recommended approach. The handling of resource/state destruction and recreation during Window resizing is also simplified and is aesthetically much more professional when done (exercising due caution to avoid deadlocks with the message pump) from outside the UI thread.
Do not be afraid of using another thread. I recognise that it's a topic most would prefer to avoid but in this instance, it's easy to employ. By simply using a Threading.Thread with the IsBackground property set to true, along with something like an "While IsRunning" style loop, you can control the flow of the main loop and gracefully exit when your application concludes.