Waiting without busy waiting

Sharky Forums


Results 1 to 4 of 4

Thread: Waiting without busy waiting

  1. #1
    Catfish
    Join Date
    Oct 2000
    Posts
    131

    Waiting without busy waiting

    Hi all,

    I have a question about waiting without busy waiting. I'm trying to create a small popup window to prompt the user for some information, and i'm wondering how to write one that will wait for the user to take some action without eating up all of the cpu cycles. In this way, i can create one of these in another part of my code, and have that code stop until the user is done entering his info.

    A good example of this is the Java object JFileChooser. Example code from the Sun site looks like:

    JFileChooser chooser = new JFileChooser();
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
    System.out.println("You chose to open this file: " +
    chooser.getSelectedFile().getName());
    }

    You can see that the function "showOpenDialog()" somehow waits for the user to do something (click ok, or cancel), and returns a value after that. How is this done without busy waiting and taking up all the processing time?

    Thanks very much!
    -Evan

  2. #2
    Tiger Shark
    Join Date
    Feb 2001
    Location
    Satan Country
    Posts
    564

    Re: Waiting without busy waiting

    Originally posted by Evan8
    Hi all,

    I have a question about waiting without busy waiting. I'm trying to create a small popup window to prompt the user for some information, and i'm wondering how to write one that will wait for the user to take some action without eating up all of the cpu cycles. In this way, i can create one of these in another part of my code, and have that code stop until the user is done entering his info.

    A good example of this is the Java object JFileChooser. Example code from the Sun site looks like:

    JFileChooser chooser = new JFileChooser();
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
    System.out.println("You chose to open this file: " +
    chooser.getSelectedFile().getName());
    }

    You can see that the function "showOpenDialog()" somehow waits for the user to do something (click ok, or cancel), and returns a value after that. How is this done without busy waiting and taking up all the processing time?

    Thanks very much!
    -Evan
    I'm half Scottish and half French.

    I surrender to alcohol.

  3. #3
    Tiger Shark
    Join Date
    Feb 2001
    Location
    Satan Country
    Posts
    564
    bah hit the mouse wheel and it posted.

    Anyhow, the filechooser is strictly modal, as far as I can tell.

    Note that this does not mean it is busy waiting.

    The current thread of execution blocks until you get a response, but all threads are not blocked. Usually GUI stuff is event driven, and anything you are running already will therefore continue runnig.

    If not, you will have to spawn it in a seperate thread, but I doubt this is the case.

    It is really only the focus that is affected - the user can't do other things without finishing the task.

    Normally, this is what you want, but if you want a non modal file dialog you will have to create it yourself by extending JDialog, which would take a few hours to an afternoon, probably.
    I'm half Scottish and half French.

    I surrender to alcohol.

  4. #4
    Catfish
    Join Date
    Oct 2000
    Posts
    131
    Hey,

    I took a look at the JDialog class, and found it very useful. I also stepped though the code of JFileChooser and did see that it blocks a thread. I'm new to multithreaded programming, so i'll use JDialog for now (might as well) and try to figure out how to block to wait for user input (just for my own programming knowledge).

    Thanks!
    -ev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •