Everything in Windows is a window, from buttons to dialogs to everything in between. Same principle still applies to WPF applications such as USD. I recently had a requirement that I needed to release some external resources in USD before the AgentDesktop was destroyed. To do that, all I had to do was to add a hook to a HwndSource and write a pseudo window procedure. The message I was interested in handling was WM_DESTROY (0x0002)
HwndSrc = PresentationSource.FromVisual(Application.Current.MainWindow) as HwndSource; HwndSrc?.AddHook(PseudoWndProc); /// <summary> /// Pseudoes the WND proc. /// </summary> /// <param name="hWnd">The h WND.</param> /// <param name="msg">The MSG.</param> /// <param name="wParam">The w parameter.</param> /// <param name="lParam">The l parameter.</param> /// <param name="handled">if set to <c>true</c> [handled].</param> /// <returns></returns> private IntPtr PseudoWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { var retval = IntPtr.Zero; switch (msg) { case Win32Helper.WM_DESTROY: // Release external resources here break; } return retval; }