views:

240

answers:

2

I’m using control that needs to be register to asynchronous events. The events will be raised in the UI thread using the ISynchronizeInvoke interface implemented by WinForms controls

I can’t register to the event at the constructor because it will allow calling the event handler before the control is fully created. during which calls to ISynchronizeInvoke are not allowed.

Solution to that problem is to use the perform the asynchronous event registration from an event handler to the HandleCreated instead of registering from the constructor.

however, this poses another issue, in some scenations the HandleCreated event is raised multiple times as result of change at the control state. For example, each changing of the “RightToLeft” property causes a WMCreate message that cause raising the “HandleCreated” event.

How can I prevent the multiply times of event rising? Is there is another way to know when the control is created and display for the first time?

I can keep a boolean flag in the HandleCreated, however it feels like a hack and I am wondering if there is a better way to handle this issue.

A: 

Can't you just override CreateHandle and check RecreatingHandle? You might also want to override DestroyHandle to catch that.

Marc Gravell
A: 

Have you considered using the Load event? Unmanaged handle is certainly available at that point in time.

GregC