MonoGame Game Loop and .NET CancellationToken Frame Interruption
0 reputation · 11 Nov 2020, 16:10 UTC
Goal
I need to cancel an in-progress MonoGame frame when a background operation’s CancellationToken is triggered, without leaving the graphics device in an inconsistent state.
Constraints
MonoGame’s Game runs Update and Draw synchronously on the main thread, and GameTime does not expose a cancellation token. Game.Exit() only stops after the current frame finishes. Throwing OperationCanceledException inside Update crashes the process because MonoGame does not catch user exceptions. Accessing GraphicsDevice from a background thread can cause backend-specific device errors. Behavior may differ between MonoGame 3.8 and later builds.
Questions
- Can a cancellation token safely interrupt a running
UpdateorDrawframe without throwing inside the game loop? - What is the supported pattern for marshaling cancellation results back to the game thread before the next frame begins?
- Does MonoGame version 3.8 or later provide any frame-level cancellation hook that avoids blocking the main thread?