Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Making one function wait for another?  RSS feed

 
Ramzi Hajaj
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys


I am just trying to figure out the best way to make one function wait for another e.g



Given this code what would be the best way to make the second call to selectcues wait until the first call has run its course? Is there anyway to do this?


Thanks alot
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to do that would be to put them on consecutive lines inside one of the methods of your class. Just as you did in the example you posted. The JVM always executes statements sequentially if they are written that way.

 
Ramzi Hajaj
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i have done that but it does not produce the desired effect. The function that is being called produces a jframe object which requires user interaction. so essentially i get two jframes, instead of the desired one jframe, wait for interaction then the other jframe?

When creating a jframe object is a new thread of execution created?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, when creating a JFrame, the user interaction is performed in a different thread - the Event Dispatch Thread (also referred to as 'the GUI thread' sometimes). If you want to block the main thread from finishing (keep the first method from finishing and the second method from starting) you don't want to use a JFrame. You want to use a Modal dialog which blocks the triggering thread. I believe JOptionPane might work if you have relatively simple interaction with the user in mind.

If the interaction is not so simple your approach may be upside down. Instead of having the main thread dictate order by creating a GUI, blocking until the GUI is finished, then creating a new GUI, you might consider using the GUI as the driver to trigger settings/configuration and eventually, when required, to destroy or hide one JFrame and display a different one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!