Jump to content
  • Advertisement
Sign in to follow this  
DvDmanDT

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

This topic is 4044 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
Advertisement
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);
System.Windows.Forms.Application.Run(win);
}



private void OnApplicationIdle(object sender, EventArgs e) {
while (AppStillIdle()) {
RenderRoot.Render();
RenderRoot.Display();
SoundRoot.Update();
if (Game != null) {
if (Game.Network != null) {
Game.Network.Recv();
}
}

}
}

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

[StructLayout(LayoutKind.Sequential)]
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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!