如何正确捕获Microsoft.Xna.Framework.Graphics.DeviceLostException?

时间:2014-11-11 16:38:16

标签: c# exception exception-handling xna draw

我使用C#和XNA开发了一款小游戏。我遇到了一个问题,当我锁定屏幕一段时间后,应用程序崩溃了:

Microsoft.Xna.Framework.Graphics.DeviceLostException

我认为异常是在Draw()例程中引发的,并试图通过以下方式捕获它:

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        try
        {
            // Draw game
            mySmallGame.Draw(gameTime);

            // TODO: Add your drawing code here
            base.Draw(gameTime);
        }
        catch (Microsoft.Xna.Framework.Graphics.DeviceLostException)
        {
            Console.WriteLine("Device lost.");
        }
    }

然而,这个例外似乎没有得到适当的处理。我将在稍后处理它,到目前为止只有“设备丢失”的文字。应该在游戏崩溃时打印出来。

堆栈跟踪是:

Microsoft.Xna.Framework.Graphics.dll!Microsoft.Xna.Framework.Graphics.GraphicsDevice.Present(tagRECT* pSource, tagRECT* pDest, HWND__* hOverride) + 0x1c6 bytes 
    Microsoft.Xna.Framework.Graphics.dll!Microsoft.Xna.Framework.Graphics.GraphicsDevice.Present() + 0x24 bytes 
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.GraphicsDeviceManager.Microsoft.Xna.Framework.IGraphicsDeviceManager.EndDraw() + 0x47 bytes    
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.EndDraw() + 0x2a bytes    
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.DrawFrame() + 0x13e bytes 
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.Tick() + 0x7a8 bytes  
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.HostIdle(object sender, System.EventArgs e) + 0x23 bytes  
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.GameHost.OnIdle() + 0x42 bytes 
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame() + 0x33 bytes 
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(object sender, System.EventArgs e) + 0x54 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(int grfidlef) + 0x3e bytes   
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) + 0x3cd bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x155 bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x4a bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes  
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.WindowsGameHost.Run() + 0x9f bytes 
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.RunGame(bool useBlockingRun) + 0x150 bytes    
    Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.Run() + 0x23 bytes    
>   Project_Ares.exe!Project_Ares.Program.Main(string[] args) Line 14 + 0xb bytes   C#

1 个答案:

答案 0 :(得分:1)

获取堆栈跟踪。这应该告诉你它发生在哪里,以及它是否甚至在你的代码中。 更新:看到堆栈跟踪,这都是您自己的代码外部的,并且最多可以通过配置选项进行修复。我强烈建议您查看MonoGame,如下所述

将异常处理放入Main方法中,因为到目前为止更容易捕获异常 - 总是首先依赖堆栈跟踪来告诉你某些东西引发了异常,&#39那是它的用途! (值得注意的例外:线程以及当任务被异常吃掉时)

尝试重新编译你的游戏而不是XNA - 后者已经停止了一段时间,前者仍然是相对活跃的开发,可能已经或可能没有解决过这个问题。