Reputation: 2392
This article suggest that .NET delays creation of windows handle but I could not find any MSDN reference etc to support this. Any one can point me to more info on it?
In an attempt to improve performance, .NET defers creation of real Win32 windows as much as possible
Upvotes: 0
Views: 96
Reputation: 941605
Yes, the native window doesn't get created until absolutely necessary. Which typically happens when you set the Form's Visible property to true or call its Show method (same thing). Or if you use a property in the constructor which requires the Handle property to be valid. Which is rare and can be a bit troublesome. The Winforms classes all store the property values you assign (or the designer generated code assigned) and don't actually apply them until the native window is created.
The optimization is usually slight but it can make a big difference on some controls. Good examples are TreeView and ListView if you give them a lot of Items in the constructor. The delayed initialization will get the underlying native Windows control with a bulk initialization message. Rather than a message for each individual item you add. Big difference. Also note that the common practice of adding the items in a form's Load event handler defeats this optimization. Always favor the constructor.
Upvotes: 2
Reputation: 134005
In the documentation for Control.Handle:
The value of the Handle property is a Windows HWND. If the handle has not yet been created, referencing this property will force the handle to be created.
This doesn't say explicitly that the handle creation is deferred, but it's very strongly implied.
Upvotes: 1