Is there any way to trigger based on process close/exit event in UWP background app?

Tyler Fonzi 1 Reputation point
2023-01-11T20:42:37.2433333+00:00

I want to have logic that triggers right before my UWP process is exited or gracefully killed. Is there anything that may accomplish what I wish to do? Or is this impossible in the UWP environment?

I know there exists a trigger for "onSuspending", but I do not want to trigger on that, I want to trigger when the process is exited or gracefully killed. I am aware of the "confirmAppClose" capability and "CloseRequested", but based on research, that seems to only be applicable for the "X" close button. I have a background task with no UI so there is not "X" button visible to trigger it. I am aware of "CoreApplication.Exiting" event but it seems like that won't work either based on what I've read.

This type of inquiry stemmed from using the type of background process associated with the Windows VPN API which apparently does not invoke disconnect [https://learn.microsoft.com/en-us/uwp/api/windows.networking.vpn.ivpnplugin.disconnect?view=winrt-22621#windows-networking-vpn-ivpnplugin-disconnect(windows-networking-vpn-vpnchannel) when the user signs out in the active session.

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 34,006 Reputation points Microsoft Vendor
    2023-01-12T06:12:35.4066667+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I have to say that currently, there is no other ways for UWP to handle the app close request than the CloseRequested event. So it is impossible to get notified when the app is forcibly closed. As you've already known, the CloseRequested event only works when user click the "X" Button.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.