C
C#13mo ago
Conner

❔ 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
Conner
Conner13mo ago
I am also using SuspendLayout() and ResumeLayout() but that doesnt seem to have any effect at all?
ffmpeg -i me -f null -
this custom control is it like a textbox or something more complex?
JakenVeina
JakenVeina13mo ago
context, please?
Conner
Conner13mo ago
Its more complex Its a panel, with labels, buttons and potentially text fields (get added depending on the data)
JakenVeina
JakenVeina13mo ago
okay and that context?
Conner
Conner13mo ago
I dont know what context you want
JakenVeina
JakenVeina13mo ago
how about what framework this is, for starters?
Conner
Conner13mo ago
Well, ok good point, its winforms, like I put in the tags
JakenVeina
JakenVeina13mo ago
...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
Conner
Conner13mo ago
Hm, how do I do profiling in VS, I dont think I have ever done that before
JakenVeina
JakenVeina13mo ago
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
Conner
Conner13mo ago
so I assume this is what got called the most
Conner
Conner13mo ago
and as I suspected its the adding of the controls
JakenVeina
JakenVeina13mo ago
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?
Conner
Conner13mo ago
I am not even calling Suspend and Resume I mean, I am, but already in the parent, as you suggested
JakenVeina
JakenVeina13mo ago
hmm well, there is ONE more piece of advice I have
Conner
Conner13mo ago
Dont use winforms :p
JakenVeina
JakenVeina13mo ago
awesome definitely a way to optimize, I just don't know it
Conner
Conner13mo ago
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
Conner
Conner13mo ago
This is now the profile and I dont think there is anything else I can do about this now
ffmpeg -i me -f null -
if there are no more identifiable bottlenecks then either you try to prepare stuff preemptively or you change the logic/architecture in the code
Accord
Accord13mo ago
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.