Results 1 to 8 of 8
  1. #1
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default How should I cancel this thread ?

    Thread exeQ suppose to load and display a picture from database.. during it's execution user sees a progressbar and a "Cancel" button... I want to cancel the thread as user press the button... so the question is how should I cancel/stop the following thread ....
    Java Code:
    Thread t = new exeQ("theName");
    t.start();
    And the Thread exeQ is
    Java Code:
    class execQ extends Thread
        {
            String DBpicName = "";
            boolean success;
            Exception x;
    
            public execQ(String picName)
            {
                DBpicName = picName;
            }
    
            @Override
            public void run()
            {
                //System.out.println("Pic - EDT: " + javax.swing.SwingUtilities.isEventDispatchThread());
                success = true;
                x = null;
                try
                {
                    //Get the requested Image from DB....
    
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery("SELECT PIC FROM Mypic WHERE NAME='" + DBpicName + "'");
    
                    //Store the picture in bytes...
                    //Move the ResultSet pointer at the 1st. record...
                    rs.next();
                    //Get the pic...
                    thePicBuffer = rs.getBytes(1);
    
                    rs.close();
                    stmt.close();
                    rs = null;
                    stmt = null;
                }
                catch(Exception e)
                {
                    success = false;
                    x = e;
                    System.err.println(x);
                }
    
                java.awt.EventQueue.invokeLater(GUItask);
            }
            
            Runnable GUItask = new Runnable()
            {
    
                public void run()
                {
                    //System.out.println("Pic gui - EDT: " + javax.swing.SwingUtilities.isEventDispatchThread());
                     // hide the ProgDlg...
                    dlg.setVisible(false);
                    dlg.dispose();
                    if(success)
                    {
                        //Create the Image and display...
                        lblImg.setIcon(new javax.swing.ImageIcon(thePicBuffer));
                    }
                    else
                    {
                        showErr(x.toString());
                    }
                }
            };
        }

  2. #2
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    No one got any clue :confused::confused:

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    If the thread doesn't have any forever loops, where is it hanging and not exiting the thread?
    If there were a loop, use a boolean that is set outside the thread and tested for inside to know to exit the loop and the thread.

  4. #4
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Hello thanks for responding...

    Quote Originally Posted by Norm View Post
    If the thread doesn't have any forever loops, where is it hanging and not exiting the thread?
    No no the thread isn't hanging at all... I just want to add the "cancel execution feature" to this thread.
    Quote Originally Posted by Norm View Post
    If there were a loop, use a boolean that is set outside the thread and tested for inside to know to exit the loop and the thread.
    Exactly but the problem is there are no loops inside the thread, and the stop() method is depreciated now. So what to do here :confused:

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Why isn't the thread exiting quickly enough for you?
    Whereever it is stalling out, there is where you need to be able to tell it to quit and exit.

  6. #6
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Hmm! well I can think of the following line :
    Java Code:
    //Get the pic...
    thePicBuffer = rs.getBytes(1);
    I think I have to do loops here .... what do you think?

    BTW what about SwingWorker ? as far as I remember it has a cancel() method, and it's also now part of java 6.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    interrupt and catch (InterruptedException)

  8. #8
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Ah! I just forgot about the thing interrupt .... I guess.
    (For past few weeks I'm constantly switching lang.)

    Yes that might do the magic.. Thank you, thank you , thank you very much. :)

Similar Threads

  1. Handlinh pdf open ,save,cancel in jsp
    By sunkaras in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2008, 09:08 AM
  2. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  3. Replies: 0
    Last Post: 01-28-2008, 07:02 AM
  4. Disable BACK and CANCEL buttons in wizard
    By consutes in forum SWT / JFace
    Replies: 2
    Last Post: 11-12-2007, 09:37 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
  •