This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem managing a loop with Swing app

 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, new to Swing. I am moving a command line application to Swing and want to manage a processing loop (stopping and starting it.) On startup the application kicks the process off and I will stop it or restart it. The stop was easy from what I can tell but I can't get the process to restart (processTestLoop.)

Here is the code sample that I am testing (sorry for the length.) Any thoughts?

Thanks
Mike



 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got a Swing concurrency issue, plain and simple. Bottom line is, when you call Thread.sleep(...) on the main Swing thread, the EDT, you prevent Swing from painting or interacting with the user, so that while the loops still runs, your program appears to grind to a halt. The solution -- use a background thread. For instance you could use a simple background thread like so:


but you should also look into trying to use a SwingWorker object in this situation too. You can read more about all of this here:
Concurrency in Swing
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also really suggest you rename your TestLoop field. When I browsed through your Main class code I thought the startLoop was a static method.
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete, Rob,

Thanks for your pointers. The testLoop field in the actual program is different, I was trying to build a test framework to make sure what track I was on. Should I make the boolean field and the stop/start methods synchronized? Other than those I do not share information.

I am reading up on SwingWorkers now. Just trying to avoid rewriting the command line program and seeing if I can fit it in. Though I may be better off rewriting portions of the logic to fit better.

Thanks
Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic