Delen via


How to resume an app (DirectX and C++)

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

This topic shows how to restore important application data when the system resumes your Windows Runtime app using DirectX with C++.

Register the resuming event handler

Register to handle the CoreApplication::Resuming event, which indicates that the user switched away from your app and then back to it.

Add this code to your implementation of the IFrameworkView::Initialize method of your view provider (named DirectXApp in the example):

using namespace concurrency;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::UI::Core;
using namespace Windows::UI::ViewManagement;

MyViewProvider::MyViewProvider() :
    // class fields
    // ...
    bool m_visible;
    bool m_windowsClosed;
{

}

void MyViewProvider::Initialize(
    _In_ CoreApplicationView^ applicationView
    )
{

   // ...

   CoreApplication::Resuming +=
       ref new EventHandler<Platform::Object^>(this, &MyViewProvider::OnResuming);

   // ...
}

Refresh displayed content after suspension

When your app handles the Resuming event, it has the opportunity to refresh its displayed content. Restore any app you have saved with your handler for CoreApplication::Suspending, and restart processing. Game devs: if you've suspended your audio engine, now's the time to restart it.

void MyViewProvider::OnResuming(
    _In_ Platform::Object^ /* sender */,
    _In_ Platform::Object^ /* args */
    )
{
    // restore app state, restart rendering/processing and refresh the display
}

This callback occurs as an event message processed by the CoreDispatcher for the app's CoreWindow. This callback will not be invoked if you do not call CoreDispatcher::ProcessEvents from your app's main loop (implemented in the IFrameworkView::Run method of your view provider).

// m_visible and m_windowsClose are class-level bool variables on MyViewProvider.

// ...

void MyViewProvider::Run()
{
    // your app's main loop!
    while (!m_windowClosed) // the app window is NOT closed
    {
        if (m_visible) // and if the app window is visible
        {
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
                                                           CoreProcessEventsOption::ProcessAllIfPresent
                                                           );
        }
        else 
        {
            CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
                                                           CoreProcessEventsOption::ProcessOneAndAllPending
                                                           );
        }
    }
}

Remarks

The system suspends your app whenever the user switches to another app or to the desktop. The system resumes your app whenever the user switches back to it. When the system resumes your app, the content of your variables and data structures is the same as it was before the system suspended the app. The system restores the app exactly where it left off, so that it appears to the user as if it's been running in the background. However, the app may have been suspended for a significant amount of time, so it should refresh any displayed content that might have changed while the app was suspended, and restart any rendering or audio processing threads. If you've saved any game state data during a previous suspend event, restore it now.

How to suspend an app (DirectX and C++)

How to activate an app (DirectX and C++)

Guidelines for app suspend and resume

Application lifecycle (Store apps)