❔ Control initialisation is very slow
I have a custom user control, used to display some data. Now, loading one of those user controls is not a problem, however, sometimes I have to load a list of these user controls (In the worst case up to 64!!). But even with only 3 or 4 loaded you can still see them getting generated and initialised.
This on its own, while an inconvenience, wouldn't be as bad but this is in combination with a combobox (to let the user select the data to display), which can be scrolled over. And the constant loading makes the combobox buffer the scroll input and delay everything even more.
I'm not sure if there is a way to make the loading faster, I can't really think of anything at least. Maybe there is a way to initialize all the user controls at once rather than one after the other?
22 Replies
I am also using SuspendLayout() and ResumeLayout() but that doesnt seem to have any effect at all?
this custom control is it like a textbox or something more complex?
context, please?
Its more complex
Its a panel, with labels, buttons and potentially text fields (get added depending on the data)
okay
and that context?
I dont know what context you want
how about what framework this is, for starters?
Well, ok good point, its winforms, like I put in the tags
...I honestly didn't know there was a tags feature
:/
I don't have any particular advice for WinForms, so my suggestion is gonna be a general "performance profile it"
a profiling capture while you're scrolling should capture where the code is spending most of its time
intuitively, the issue is that something's probably triggering a lot of unnecessary or redundant redraws, and a profile capture might be able to highlight that
Hm, how do I do profiling in VS, I dont think I have ever done that before
within an active debug session, you should have a "Diagnostic Tools" widget, usually in the same pane as "Solution Explorer"
"Record CPU Profile" in there should be what you want
the output is not easy to read if you've never done it before, but it gives you a breakdown of how much time the CPU is spending in different methods
the practical approach is to look for the "hottest" spots, and then go to the code for those and think that through more thoroughly
might not be terribly applicable to you, cause the hottest stuff is probably gonna be not-your-code
but it might hint at what you're inadvertently telling WinForms to do too much of
so I assume this is what got called the most
and as I suspected its the adding of the controls
sure
but what part of it
21% for .ResumeLayout() seems maybe a little sus?
could you potentially bump that up a layer and call
SuspendLayout()
and ResumeLayout()
just once, on the parent of these controls?I am not even calling Suspend and Resume
I mean, I am, but already in the parent, as you suggested
hmm
well, there is ONE more piece of advice I have
Dont use winforms :p
definitely a way to optimize, I just don't know it
oh I found the suspend layout
its in the designer.cs of the user control
I didnt know that InitialiseComponent references that
Ok even though VS told me to not remove that under any circumstance (lol) I did anyways
and now its obviously gone in the profiling and I didnt see any problems with the ui
Performance is still rubbish though
This is now the profile and I dont think there is anything else I can do about this now
if there are no more identifiable bottlenecks then either you try to prepare stuff preemptively or you change the logic/architecture in the code
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.