multiple occurences of HandleCreated for a single control
- by Asher
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.