Results 1 to 7 of 7
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default event that terminate function (without Global flags)

    Hi

    I have gui that is activate in a function. ( like progress bar with stop button)

    I i make event that cause the original function to quit ?

    the gui function ( its not code- just structure)

    * create GUI vindows and set visible true
    * register listener , i.e. : stopButton.addActionListener(new StopEvent() );
    * start some lop
    * * action in lop
    ** other lop ....
    ..

    now StopEvent should be something like that

    Java Code:
    public class StopEvent implements ActionListener  {
        public void actionPerformed(ActionEvent e)
    {
     //     cause the above function  - to exit
    }
    
    }

    Note - I can use some global variable , or link some variable in the StopEvent, . and add a lot of 'if (stopFlag == 1 ) return ' but that is very ugly solution
    Last edited by itaipee; 12-01-2009 at 09:56 AM.

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Testing a boolean like stopFlag is the recommended way to stop a thread.

  3. #3
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default Problem with the stopFlag

    stopFlag can be problematic if the lop is too complex - and the event need to terminate right away and not at the end of the lop.

    I could use something like that
    Java Code:
    // assuming I have event that change stopFlag to 'true
    while (1==1)
    {
    if (stopFlagg==false)    proc1(...); else return false;
    if (stopFlagg==false)    proc2(...); else return false;
    if (stopFlagg==false)    proc3(...); else return false;
    if (stopFlagg==false)    proc4(...); else return false;
    if (stopFlagg==false)    proc5(...); else return false;
    ..
    ..
    Also inside of this procX I might need this "if (stopFlagg==false) " for other function.

    I am looking for more elegant solution
    Last edited by itaipee; 11-29-2009 at 08:18 AM. Reason: fix the example code

  4. #4
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    no -one ? is it imposible task what i have asked ?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    is it imposible task what i have asked ?
    Have you read the Thread.stop() API and the reason why the stop() method has been deprecated?

  6. #6
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Quote Originally Posted by zweibieren View Post
    Testing a boolean like stopFlag is the recommended way to stop a thread.
    I agree. You can say that maybe is quite time comsuming but I think is more elegant than a simple break.

  7. #7
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Have you read the Thread.stop() API and the reason why the stop() method has been deprecated?
    thanks , camickr

Similar Threads

  1. Retry constructing a thread/ terminate thread
    By coyote1982 in forum New To Java
    Replies: 6
    Last Post: 09-17-2009, 09:38 AM
  2. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 06:16 AM
  3. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  4. 3 errors and then terminate program
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 05-20-2008, 01:57 AM
  5. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:56 PM

Posting Permissions

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