Sign in to follow this  
Moe

[MDX] Simple app using 50% of cpu...

Recommended Posts

I seem to have run into a strange problem while learning to use Managed DirectX. I have been canabalizing several tutorials in an effort to learn my way around MDX. Anyway, the problem is that my app is a total CPU hog - using 50% of my CPU (I suspect it would use 100%, but I have a P4 with Hyperthreading, and it is only hogging one thread, for the most part). I suspect that it somehow deals with my main loop/message handling. Here is the main chunk of code that does all that:
//-----------------------------------------------------------------------------
// File: Main.cs
// Desc: Contains the main entry point for the application, and some other stuff
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Project3
{
    public class frmMain : Form
    {
        // Global variables go here... yes, I know they are bad
        Engine.Engine engine;

        public frmMain()
        {
            //TODO - read the size values in from the settings file

            //set up window properties
            this.ClientSize = new System.Drawing.Size(640, 480);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.Text = "Project3";
        }

        public bool InitializeEngine()
        {
            //TODO - read in properties from settings file
            engine = new Engine.Engine();
            return engine.Initialize(this, true, 800, 600, 4, false);
        }

        private bool Render()
        {
            return engine.MainLoop();
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render(); // Render on painting
        }

        //TODO - add code in here to pass input messages to the engine
        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
                this.Close(); // Esc was pressed
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {

            using (frmMain frm = new frmMain())
            {
                if (!frm.InitializeEngine()) // Initialize Direct3D
                {
                    MessageBox.Show("Could not initialize the engine.  This program will now exit.");
                    return;
                }
                frm.Show();

                // While the form is still valid, render and process messages
                while (frm.Created)
                {
					if (!frm.Render())
					{
						MessageBox.Show("Error during main loop.  Program will now exit.");
						return;
					}
					Application.DoEvents();
                }
            }
        }
    }
}




The odd thing is that the Main() is nearly identical to the DirectX SDK Managed tutorial samples, yet they use 0 - 2% CPU usage. Eventually I want to switch it over to use this message loop system, but I want to get things working properly right now before I do that. Can anyone spot why this might be eating my CPU alive? EDIT - yes I know I posted this in the DirectX forum. I figure its a little more related to rendering things with Managed DirectX than .Net stuff.

Share this post


Link to post
Share on other sites
I don't see anything obviously wrong with your loop ...so at a guess, have you set your PresentParameters.PresentationInterval to PresentInterval.Immediate?

If this is the case, setting it to PresentInterval.One would cause it to sync with your monitor's refresh and would result in less CPU usage. Setting it to Default would result in even less CPU usage as it isn't quite equivalent to One

Sorry if I'm way off on this one [smile]

All the best,
ViLiO

Share this post


Link to post
Share on other sites
Thanks for the reply ViLiO.

You are bang on. It's funny because I have the code in place to turn Vsync on or off at startup (It's the last parameter of engine.Initialize()[smile]). I tried switching it off and it is now using 0 - 2% CPU usage. Good call! It hadn't even crossed my mind why it was doing that.

A Rate++ for you!

EDIT - a funny note: While I was posting the reply to this thread, I minimized the app that I was having problems with (after turning vsync on). While minimized it was using 50% cpu usage, until I restored it, then it dropped back to 0 - 2%. Go figure[smile]. I think I will try using the code from the link that I posted earlier and see what sort of things it does. I also still need to be checking the Cooperative Level to see if the device is lost, so that probably has something to do with it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this