Question Delay in inserting images in UserControl displayed in TabItem

Elad Yehuda

Member
Joined
Jul 3, 2023
Messages
9
Programming Experience
3-5
Hello everyone!
I have an annoying problem, I have a UserControl that contains a list of questionnaires, each questionnaire contains several questions, and the answers themselves are questions are actually a collection of images that are dynamically built in the code behind, that is, I run on a BitmapImage dictionary
whose value is the BitmapImage of the image itself and the key is the name of the image itself. I wrap the image itself in a Border and then insert the Border into the UnifromGrid (I have a reason not to use with ItemsControl even though it's super useful). Now in the main window I add this UserControl and embed it in one TabItem out of 3 in the TabControl, the problem is this, the UserControl itself contains a property that binds with some property from the MainWindow, this property takes care of running a function that should fill the -UnifromGrid in images, this means that as soon as the window comes up, the images actually appear on the UnifromGrid, now when I click on its TabItem there is a 2 second delay that just causes the program to freeze, this means that I can't have focus on any other control in the window And I have to wait, first question why this delay is created while this TabItem is in focus for the first time during the program
(although after that if I exit this window and restart it this delay won't repeat itself even though I reupload the images again)? And this despite the fact that when the window is loaded, the images are already on the UnifromGrid!
The second question is how can this be overcome?
It is important for me to note that when loading the program while still in the App, I turn to the function that fills in the images for me in a dictionary that saves the images as a BitmapImage variable and for each image I perform optimization with a decoding function.
Another important thing to note is that I only upload 24 images in total.
Thanks to the respondents!
 
Run your code under the profiler and identify where the time is being spent. Anything else we say will just be speculation. The profiler will tell you exactly where that time is spent.

Speculation:
My first guess would have been that you have some code that does lazy initialization and all that initialization happens right when that tab is shown. But you also stated that it is only slow on first run. If you run again, then it is fast the second time. At least that is how I am interpreting "although after that if I exit this window and restart it this delay won't repeat itself even though I reupload the images again". If by "restart" you mean it's the same running process, but you just open the window again, then the lazy initialization is in play again.

If by "restart" you mean that you actually stop the process and then run it again, then it's something else. This could be your hard drive hardware. One of my drives is still an old spinning platter type. It's slow on first read/write if it hasn't been accessed in a few minutes, but it is fast if I do multiple accesses while it is running at speed. If you have an SSD, but you actually pull the images from the Internet or the network, some caching maybe being done on your behalf by the network drivers and/or the web/file server.

Again, I recommend running with a profiler. Once you identify the hotspots and the code that calls those hotspots, post them here so that can actually see your code and possible give some advice. Anything else is just like asking a friend who happens to be a doctor for advice while at the golf course, instead of going in for a doctor visit.
 
Back
Top Bottom