• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem stopping GUI thread in run().

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I hope this is not one of those " Oh My God, EVERYBODY knows that!" kind of questions. But implementing threads is new for me.

I have a GUI application that is allowed to spawn new copies of itself, which then in turn can spawn more new copies, etc.. No problem there, that functionality works. My problem comes when trying to shut them down. Each spawned GUI application must be able to terminate itself without terminating any of the other copies, "killSelf". They must each also be able to terminate all running GUI applications, "killAll". The killAll part is easy, any GUI copy calling System.exit() stops the JVM, so they all will stop.

My question is, how do I implement "killSelf"? I had thought that when a thread is started inside run(), and then when run() finishes that thread should terminate. But in the code below, when run() returns and the message "exiting run()" is printed to the console, the new MyGUI application is still running. How can I get it to terminate?

The commented out code was going to be how I stopped the new MyGUI, but it didn't work.

Here is a sketch of my code:



Any suggestions whould be appreciated. Perhaps there is a pattern I should be following?

Thanks,

Rick Stewart

[ September 30, 2006: Message edited by: Rick Stewart ]

[ September 30, 2006: Message edited by: Rick Stewart ]

[ September 30, 2006: Message edited by: Rick Stewart ]
[ September 30, 2006: Message edited by: Rick Stewart ]
 
Rick Stewart
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry the code looks like heck, I couldn't get tab to work, and when I posted the message, all the leading line spaces that were formatting the code were removed.

Rick...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We've got these "[ CODE][ /CODE]" tags you can use to preserve formatting in code; see the "CODE" button below the text entry box.

Now, as to your problem: I see a whole bunch of different places where startGUI is going to be called, which doesn't seem right; you want to build a window just once, right? But let's assume you have a handle on that.

If you build a Frame in a Thread, then that Frame will outlive that Thread. There's no special relationship between the Thread and the Frame; when the Thread dies, the GUI remains. Since you don't show a lot of the critical code, I can't say for sure, but the Thread probably exits as soon as the Frame is all put together, anyway.

To dispose of a Frame, call (wait for it...) the dispose() method of the Frame. If your Frame has some extra cleanup to do, then override dispose(), add the extra code, and then call super.dispose().
 
Rick Stewart
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ernest, thanks for your reply, and the tip about using , that sure makes everything look better!

There are just 2 places where startGUI() gets called, the first is in main() when I first run "c:\>java MyGUI", and then thereafter its only ever called from inside run() when I start up a new copy.

As you suggested I've looked up Window dispose(), and it is just the thing I was looking for I think. I have lots of books that explain threads and run(), but all the examples dealt with simple threads, and none that I saw dealt with a thread that would live past the life of the run() method.

Thanks very much,

Rick Stewart
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick,

Note that the thread does not live past the end of the run() method. The statements in the run() method execute, and then the thread exits. But its side effects -- such as creating some GUI objects -- live on. Imagine a Thread that calls System.out.println() in its run method. When the thread terminates, you don't expect the messages to somehow become "unprinted", do you? Same thing.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try writing a stop method ...


synchronized void stop()
{
spawn=null;
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic