07-05-2009 12:31 PM
I have an app that makes updates to the UI by passing in a reference for a bitmapfield to a new thread, and that thread updates the UI.
The problem I am having is that I want the main thread "wait" for the sub-thread to update the graphics, before proceeding with the "game".
so.... for example..
// update the main bitmapfield
pt = new PlayerThread(bfField,square); pt.start();
// wait for the thread to finish updating the ui
Now, I *KNOW* this is not the proper way to do this, because the UI does not get updated, since the UI is in a loop waiting for the thread to finish, which defeats the purpose of having the thread in the background in the first place.
What is the "correct" wait to do this? Have I completely missed the point of threading?
This works fine if I don't "wait" for the thread,but it means the user can press buttons, or interact with the game when they aren't supposed to.
I was thinking there must be a way to set a "isthreading = true" var, and disable button presses by adding an if to my keydown, but how can i make my app "wait" for the thread i created to be finished, but not actually pause the UI itself?
Hopefully that was clear...
Solved! Go to Solution.
07-05-2009 12:34 PM - edited 07-05-2009 12:39 PM
I just noticed Thread.yield(); ...
is that the proper way? ... edit, this has the same effect as the while loop..
07-05-2009 01:27 PM
You need a notification mechanism that tells the UI thread "I'm done" or "data is ready". Never loop in a UI thread - this will eventually cause the event queue to overflow, resulting in you application being dumped out as "unresponsive".
A typical mechanism is to implement a listener and a data event. The "interested" party registers a listener with the background thread, the background thread fires the data event when appropriate.
If there is only one listener, then this can be implemented with less overhead by using a callback mechanism.
Look for references to the Observer pattern: