'IBackgroundTaskInstance.Canceled is called even when gracefully ending process
In my UWP C++/WinRT app I launch a full-trust C# process and register a handler for the IBackgroundTaskInstance.Canceled event.
auto winrt::myApp::implementation::App::OnBackgroundActivated(const BackgroundActivatedEventArgs& args) -> void
{
if (const auto details { args.TaskInstance().TriggerDetails().as<AppServiceTriggerDetails>() }; details != nullptr)
{
if (details.CallerPackageFamilyName() == Package::Current().Id().FamilyName())
{
this->appServiceDeferral = args.TaskInstance().GetDeferral();
args.TaskInstance().Canceled([this](const IInspectable&, const BackgroundTaskCancellationReason& r) -> winrt::fire_and_forget
{
if (this->appServiceDeferral != nullptr)
{
this->appServiceDeferral.Complete();
}
// handle the error (show a message etc.)
});
}
}
}
The idea is to show an error message or otherwise handle the case when the C# process ends unexpectedly.
However, above event handler is executed even when the C# process exits gracefully, i.e. by return 0;.
In that case, the BackgroundTaskCancellationReason is Terminating which MSDN explains as follows:
The background task was canceled because the application is about to be terminated due to system policy. The application should save any state that will be needed when the application restarts.
Am I misunderstanding something? How can I distinguish between successful, graceful exit of the full-trust process and an unexpected termination?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
