Udostępnij za pośrednictwem


WindowsXamlManager.XamlShutdownCompletedOnThread Event

Definition

Occurs when the XAML runtime has finished its shutdown process on the current thread.

// Register
event_token XamlShutdownCompletedOnThread(TypedEventHandler<WindowsXamlManager, XamlShutdownCompletedOnThreadEventArgs const&> const& handler) const;

// Revoke with event_token
void XamlShutdownCompletedOnThread(event_token const* cookie) const;

// Revoke with event_revoker
WindowsXamlManager::XamlShutdownCompletedOnThread_revoker XamlShutdownCompletedOnThread(auto_revoke_t, TypedEventHandler<WindowsXamlManager, XamlShutdownCompletedOnThreadEventArgs const&> const& handler) const;
public event TypedEventHandler<WindowsXamlManager,XamlShutdownCompletedOnThreadEventArgs> XamlShutdownCompletedOnThread;
function onXamlShutdownCompletedOnThread(eventArgs) { /* Your code */ }
windowsXamlManager.addEventListener("xamlshutdowncompletedonthread", onXamlShutdownCompletedOnThread);
windowsXamlManager.removeEventListener("xamlshutdowncompletedonthread", onXamlShutdownCompletedOnThread);
- or -
windowsXamlManager.onxamlshutdowncompletedonthread = onXamlShutdownCompletedOnThread;
Public Custom Event XamlShutdownCompletedOnThread As TypedEventHandler(Of WindowsXamlManager, XamlShutdownCompletedOnThreadEventArgs) 

Event Type

Examples

This example demonstrates how you might subscribe to the event to clean up your objects when XAML is no longer running on the thread.

In this example you have some different types of objects you want to clean up as the app shuts down. XAML has some indirect references to some of these objects (for example, "Model" objects like in an Model-View-ViewModel app), so you don't want to destroy these until XAML is finished with its work. You also have some objects that are getting cleaned up in another process, and you want to wait for those as well.

So, you use the XamlShutdownCompletedOnThread event and its deferral to organize the shutdown process. When this event is raised, you know XAML is done using your objects on this thread. You can also take a deferral so that shutdown won't complete until your remote operations are finished.

WindowsXamlManager manager = WindowsXamlManager::GetForCurrentThread();
manager.XamlShutdownCompletedOnThread([](
    const WindowsXamlManager& sender,
    const XamlShutdownCompletedOnThreadEventArgs& args) -> IAsyncAction
    {
        // Once we get this deferral, the DispatcherQueue shutdown process
        // won't continue until Complete is called.
        // Until we call deferral.Complete(), we can still use the 
        // DispatcherQueue and give it new work.
        auto deferral = args.GetDispatcherQueueDeferral();

        // Now that XAML has shutdown, we can clean up any of our objects
        // that XAML might have been using.
        CleanupUIThreadObjects();

        // Capture the UI thread context.
        winrt::apartment_context ui_thread;

        // We can also do cleanup work that might take a while. For example, 
        // we can wait for work in other processes to finish.
        co_await CleanupRemoteObjects();

        // Switch back to the UI thread, in case we have any UI-thread work left to do.
        // It will still be running because we took the deferral.
        co_await ui_thread;

        // Done! Shutdown may continue.
        deferral.Complete();
    });

Remarks

Shutdown of the XAML runtime is tied to the shutdown sequence of the DispatcherQueue running on the thread. For more info, see the DispatcherQueue documentation.

When a DispatcherQueue on a thread that uses XAML is shutdown, these events are raised in order:

At the time the WindowsXamlManager.XamlShutdownCompletedOnThread event is raised:

  • XAML has unloaded all the live XAML objects and raised the Unloaded event for each object.
  • XAML no longer has any state associated with the current thread. WindowsXamlManager.GetForCurrentThread returns null at this time.
  • The DispatcherQueue on the current thread is still available and usable. It is in its shutdown sequence, so note the ShutdownStarting event has already been raised and will not be raised again.

Note

Even if you call Close on the WindowsXamlManager object or release all your references to it, this event will still be raised.

Applies to