WindowInteropHelper.Handle — do I need to release

2019-03-06 07:46发布

问题:

In WPF I'm getting IntPtr handle using this code:

IntPtr mainWindowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle

When I finish using this handle, do I need to release it in anyway (e.g. using Marshal.FreeHGlobal() method) ?

EDIT: I was thinking about Marshal.FreeHGlobal(), not Marshal.Release(), sorry!

回答1:

This is not in any way related to COM, Marshal.Release() does not apply. You simply get a copy of the native window handle, it does not have to be released. You need to stop using the handle when the window is destroyed. Signaled by the Window.Close event.



标签: wpf winapi com