Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a window with a combobox and a few other controls on and the combobox loads data from an xml file into labels to show to the user. The xml that is loaded changes depending on the selected index of the combobox. I also have a user control that I have made to look like a splash screen but it is for long operations so it has an image of a compass (don't ask why) and text to say 'Loading . . .'. Whenever the user selects an item in the combobox I want the usercontrol to become visible over the whole of the window so it covers it (it is already in place so the visibility just needs to be changed). Then the operation will complete and the usercontrol will become hidden.

It seems simple to do and I am currently changing the visibility on the selectionchanged event of the combobox but that it also where the large xml file is read. The problem I am having is that the code is executed fine but it does not have enough time to draw the usercontrol when the visibility is changed. SO it changes the visibility and thinks that it is then visible on the screen and moves on to the file reading when in fact the usercontrol's property is set to visible but that actual graphics has not been drawn to the screen so it then removes the purpose of having that waiting screen because it is never drawn. How can I avoid this so when the index is changes, the usercontrol is drawn to the screen before it moves on? Telling the thread to sleep would not work because it would be pausing the thread after it thinks it is drawn so it would only delay the finishing of the xml reading. I hope I have not explained it too much, thank you.
Posted

1 solution

It sounds like this is standard GUI thread starvation.

When you set the user control visible, it doesn't do it straight away. Instead the request gets posted onto a message queue. The thread which would read this and process the request is the same thread which is reading the xml file, but it can't actually do that because its busy reading the xml instead.

What you should do is the reading of the xml (or anything which takes more than a brief moment) on a background thread. This leaves the GUI thread free to actually attend to GUI stuff. Take a look at the BackgroundWorker class - this will be perfect for what you are trying to do.
 
Share this answer
 
Comments
Henry Hunt 30-Dec-13 13:17pm    
Isn't there any way I can avoid using another thread? All I want to do is change the visibility of a control and wait for it to render before continuing, is it really that hard?!
Dave Kreskowiak 30-Dec-13 13:25pm    
Did you want to do it correctly (background thread) or did you want to hack a crap solution together that may not work in every situation (Application.DoEvents)??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900