Advertisement Jump to content
Sign in to follow this  

[.net] [MDX] How to not block the event thread in a sane way?

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, what is the correct way to initialize MDX (D3D) to not block the event thread of winforms? I've seen people use Timers for their game loop, and I've seen people using OnPaint and invalidate, but both ways seem kinda bad. I would like to use my own loop for updating, rendering etc. I've seen Application.DoEvents(); but I hear it's VERY slow.. Should I limit it to be called once every second? Should I run my entire game in another thread?

Share this post

Link to post
Share on other sites
The 'proper' way I've seen is to hook your game loop into Application.Idle. There was a link, but I can't seem to find it...

Anyways, something akin to this:

// Main...
System.Windows.Forms.Application.Idle += new EventHandler(win.OnApplicationIdle);

private void OnApplicationIdle(object sender, EventArgs e) {
while (AppStillIdle()) {
if (Game != null) {
if (Game.Network != null) {


private static bool AppStillIdle() {
pkMessage msg;
return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);

public struct pkMessage {
public IntPtr hWnd;
public Message msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;

[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out pkMessage msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!