Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hi i have following code and it is enough just if i click on blue window then cpu goes to 100% for like at least one minute even with my i7 4 cores. I just check even with empty project and is the same !!!

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    private Texture2D cursorTex;
    private Vector2 cursorPos;
    GraphicsDevice device;

    float xPosition;
    float yPosition;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);

        Content.RootDirectory = "Content";

    }

    protected override void Initialize()
    {
        Viewport vp = GraphicsDevice.Viewport;

        xPosition = vp.X + (vp.Width / 2);
        yPosition = vp.Y + (vp.Height / 2);
        device = graphics.GraphicsDevice;
        base.Initialize();
    }

    protected override void LoadContent()
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);
        cursorTex = Content.Load<Texture2D>("strzalka");
    }


    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }


    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {

        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(cursorTex, cursorPos, Color.White);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
share|improve this question
4  
seems this is the answer stackoverflow.com/questions/8553998/high-cpu-usage-with-xna –  kosnkov Apr 8 '12 at 19:59
    
That is only the answer for some CPUs. Releasing a product with fixedtimestep set to false by default will generally be bad, as it will cause the program to try and achieve the highest framerate possible, which will usually result in much higher CPU and GPU usage. –  Nic Foster Sep 10 '12 at 21:37
    
Could this be an anomaly? Bad DirectX install? Windows having fun at your expense? Visual Studio having a panic-attack? –  Cypher Sep 10 '12 at 22:48
    
@Cypher: Good question. I've never had issues with the fixed time step, it's always worked for me as advertised. The link provided in the first comment to the OPs post is the first I've heard of people having to turn off the fixed time step to actually lower CPU usage. –  Nic Foster Sep 10 '12 at 23:30

2 Answers 2

Having a single core go to 100% while running the game loop is common, even in the boilerplate project. Games run a continual update/draw loop. Though you'll see different frequency for Update() vs Draw() depending on what your game is doing, they're both running in a fast loop.

If it's starting after the mouse click, I'm guessing it's because that's when the app becomes active.

share|improve this answer

Answer for most XNA users: This is because your game is probably running at the highest framerate it can. You can fix the framerate to solve this problem.

Inside of your function public Game1() place this code: this.IsFixedTimeStep = true

For some CPUs setting IsFixedTimeStep to true will not solve the issue. This is an XNA problem. Setting it to false will cause the application to loop with no delays between frames, allowing the framerate to go as high as the computer can get it to go. This will cause GPU usage to be higher for any users of your application, and higher CPU usage for any user that isn't experiencing the issue that the OP is.

Answer for the OP: XNA has issues with certain CPUs it seems like, in your specific case you'll want to set this.IsFixedTimeStep = false

It's also worth noting that even with IsFixedTimeStep is set to false, when the XNA window loses focus it will revert back to a fixed time step so that the application doesn't kill the performance of other applications when it's not in focus.

share|improve this answer
    
IsFixedTimeStep It's by default set on true. –  Fuex Apr 9 '12 at 22:21
2  
@Fuex, indeed that is the case, but I don't see any way that a simple program like that would consume 100% CPU unless it was running without frame limits. By default XNA will limit the framerate when the window doesn't have focus, so his explanation of the framerate jumping when the window is clicked seems to suggest it may be that the framerate is not fixed. –  Nic Foster Apr 10 '12 at 15:04
1  
The answer in the comment of OP notes that it should be set to False not True. –  joltmode Aug 7 '12 at 6:12
    
As Tom says, this is the wrong answer, the right one is in the comment by kosnkov. –  Tristan Warner-Smith Sep 6 '12 at 14:05
1  
More information on what Game.FixedTimeStep actually does: blogs.msdn.com/b/shawnhar/archive/2007/07/25/… –  Cypher Sep 10 '12 at 22:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.