It starts with “Can not create Window Handle”… and continuous random crashes. You find out your app reaches the max of 10000 user objects. (Just check the task manager, you’ll see I’m right.)
So, you’ve been using your own usercontrols on a flowlayoutpanel; you add, you remove, you clear… however the user objects count keeps growing and something is leaking somewhere…
You find that .controls.clear() does not work, you have to dispose() of all the objects first. Okay, do that, but it still doesn’t work.
So what’s the trick?
Simply remove first, before the dispose, and do this one at a time.
Just use the following routine:
Public Sub ClearFlowLayoutPanel(cFlowLayoutPanel As FlowLayoutPanel) Do While (cFlowLayoutPanel.Controls.Count > 0) Using controltoremove = cFlowLayoutPanel.Controls(0) cFlowLayoutPanel.Controls.Remove(controltoremove) controltoremove.Dispose() End Using Loop GC.Collect() GC.WaitForPendingFinalizers() cFlowLayoutPanel.Controls.Clear() End Sub
Good Luck! …or ask My Brain.
sources or more info: