GUI locking up, but code running fine.

  • Thread starter prolifekilledmywife
  • Start date
P

prolifekilledmywife

I'm using Netbeans 5.0 beta for an IDE, with swing components. The
situation is something like this:

The program is a board game - the user chooses a building from a panel,
and then is supposed to get a message to choose what resource to pay
for it with. Here's a trace of the code:

Game (main object):
....
board.activate():
Game.game.setPlayer(worker);
Game.game.playerMessage("Choose a wooden building
from the building panel.");
Game.game.waitForBuild(type) :
built = false;
state = type + Game.BuildWood -
1;

mainPanel.buildings.getBP().setSelectedIndex(type-1);
while(!built)
{ Thread.yield(); }
No problems yet. This works fine, unless a building that needs to call
chooseResource is selected.
So, the user clicks one of these buildings, which activates the
following code, which is where the freeze occurs:

String r = Game.game.chooseResource():
state = Game.chooseResource;
resource = "";
while(resource.length()==0)
Thread.yield();
return resource;

Now chooseResource does work in any other context - resource gets set
by a mouseClicked event handler in a panel out there. But at this
point, the GUI stops responding. The playerMessage never gets printed,
and the click event never gets triggered. I did some investigating,
printing out numbers inside the two inmost loops, and the
chooseResource loop is continually running. I know the code's ugly,
but even so, the cause of this behavior is beyond me. Any suggestions
would be greatly appreciated!
 
C

Chris Smith

String r = Game.game.chooseResource():
state = Game.chooseResource;
resource = "";
while(resource.length()==0)
Thread.yield();
return resource;

Now chooseResource does work in any other context - resource gets set
by a mouseClicked event handler in a panel out there. But at this
point, the GUI stops responding. The playerMessage never gets printed,
and the click event never gets triggered. I did some investigating,
printing out numbers inside the two inmost loops, and the
chooseResource loop is continually running. I know the code's ugly,
but even so, the cause of this behavior is beyond me.

The loop runs forever because nothing is causing resource.length() to
return anything other than 0. It blocks the GUI because this code is
running in the thread that does all the GUI stuff. Remaining GUI work
is all queued up waiting for this code to finish so that it has a chance
to run. You should move the long-standing work into a separate thread
(although there really isn't long-standing work, just a busy-wait loop.)

This is a case where ugly code isn't something you should try to
convince us to ignore so we can get to the real problem. Ugly code is
the cause of the problem. Take some pride in your work, and do things
right. If you know this is bad code, don't write it.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
P

prolifekilledmywife

Thanks for the response, Chris. I sorted it out though - the problem
was actually that the GUI wouldn't do anything until the mouseClicked
event returned. Make sense, I guess. Resource does get set by the
mouseClicked method of the player board - I guess I probably should
have mentioned that as well. Anyway, thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top