tags:

views:

404

answers:

4

When is this called? More specifically, I have a control I'm creating - how can I release handles when the window is closed. In normal win32 I'd do it during wm_close - is DestroyHandle the .net equivalent?

A: 

Normally DestroyHandle is being called in Dispose method. So you need to make sure that all controls are disposed to avoid resource leaks.

aku
A: 

Dispose does call DestroyHandle, but not always. If I close the parent window, then Windows will destroy all child windows. In this situation Dispose won't call DestroyHandle (since it is already destroyed). In other words, DestroyHandle is called to destroy the window, it is not called when the window is destroyed.

The solution is to override either OnHandleDestroyed, or Dispose. I'm opting for Dispose.

dan gibson
A: 

@dan gibson,

out of curiosity, why do you want to destroy handle yourself? In my practice I don't remember a case when I was needed to call DestroyHandle explicitly. Could you please explain your case?

aku
+1  A: 

I don't want to destroy the window handle myself - my control is listening for events on another object and when my control is destroyed, I want to stop listening to those events. Eg:

void Dispose(bool disposing)
{
    otherObject.Event -= myEventHandler;
}
dan gibson