Search code examples
c++windows-phone-8terminate

How to use “Application.Current.Terminate” using cpp on WP8


I am writing a C++ DirectX application without XAML for Windows Phone 8. I handled the back key because I want to close the app on manual, but I find no API for cpp.

C# has official API System.Windows.Application.Current.Terminate(), but cpp has no namespace System::Windows::Application.

I've search a lot but I can only find the C# method. I've tried Windows::UI::Core::CoreWindow::GetForCurrentThread()->Close();
It can close app, but it causes a Platform::COMException, and it can't be resolved by try-catch.

////////////////////update 1/////////////

well i find

int b = 0.1*10 - 1;
int a = 1/ b;

app will exit too. so Windows::UI::Core::CoreWindow::GetForCurrentThread()->Close(); is not a way for close. maybe it is just a crash.

////////////////////update 2/////////////

I find a API

Windows::ApplicationModel::Core::CoreApplication::Exit();

but doc says that

"Shuts down the app. Do not use this method to shut down an app outside of testing or debugging scenarios."

so I think it will not pass the test.

////////////////////update 3/////////////

if use Windows::ApplicationModel::Core::CoreApplication::Exit(); to close app. and then if i click the icon to restart the app. it will cost a very very very long time to start. may be Windows::ApplicationModel::Core::CoreApplication::Exit() close some system service. but if use exception to close app it will not cost a long time to start.

////////////////////update 4/////////////

if use Windows::ApplicationModel::Core::CoreApplication::Exit(); in simulator it can not restart app by click icon again


Solution

  • All you need to do to exit the app is break out of the CoreApplication::Run while() loop.

    For instance for the default template cube DirectX sample app, all you need to do is set m_windowClosed member variable to true.

    So if you want the app to end if you tap the screen, do...

    void PhoneDirect3DApp1::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
    {
        m_windowClosed = true;
    }
    

    However as the comments say you must take care to handle the back button appropriately or else you'll fail cert.