XNA应用程序在休眠后崩溃

XNA应用程序在休眠后崩溃,xna,power-management,Xna,Power Management,我创建了简单的XNA全屏应用程序。如果它处于活动状态,并且在恢复后系统进入睡眠模式或休眠模式,则应用程序将崩溃。我怎样才能解决这个问题 UPD: 我为系统消息添加了处理程序 public Game() { SystemEvents.PowerModeChanged += OnPowerChange; this.graphics.PreferredBackBufferWidth = 1920; this.graphics.PreferredBackBufferHeight

我创建了简单的XNA全屏应用程序。如果它处于活动状态,并且在恢复后系统进入睡眠模式或休眠模式,则应用程序将崩溃。我怎样才能解决这个问题

UPD: 我为系统消息添加了处理程序

public Game()
{
    SystemEvents.PowerModeChanged += OnPowerChange;
    this.graphics.PreferredBackBufferWidth = 1920;
    this.graphics.PreferredBackBufferHeight = 1080;
    this.graphics.PreferredBackBufferFormat = SurfaceFormat.Bgr565;
    this.graphics.IsFullScreen = true;
}
void OnPowerChange(Object sender, PowerModeChangedEventArgs e)
{
    switch (e.Mode)
    {
        case PowerModes.Suspend:
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.ToggleFullScreen();
            graphics.ApplyChanges();
            break;
    }
}
若应用程序处于窗口模式,则OnPowerChange()工作正常,并将分辨率从1920x1080更改为800x600。但若应用程序是全屏模式,则不会调用此方法。此外,我在这里得到运行时错误

static void Main(string[] args)
{
    using (Game game = new Game())
    {
        game.Run();
    }
}

错误是:无法访问已释放的对象。对象名称:“WindowsGameForm”。

您必须检测系统何时进入睡眠状态(应该有事件)并停用游戏,然后在游戏有焦点时重新激活。例如,我尝试了此解决方案,但在调用处理程序之前应用程序崩溃。若应用程序是窗口模式,那个么处理程序可以正常工作。我在另一个问题中读到了一个类似的问题,那个就是让处理程序全屏运行