Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-21-2008, 02:03 AM
Member
 
Join Date: Aug 2008
Posts: 51
playwin2 is on a distinguished road
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 ....
Code:
Thread t = new exeQ("theName"); t.start();
And the Thread exeQ is
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()); } } }; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-22-2008, 01:41 AM
Member
 
Join Date: Aug 2008
Posts: 51
playwin2 is on a distinguished road
No one got any clue
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-22-2008, 02:20 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-22-2008, 02:36 AM
Member
 
Join Date: Aug 2008
Posts: 51
playwin2 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-22-2008, 04:40 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-22-2008, 06:00 AM
Member
 
Join Date: Aug 2008
Posts: 51
playwin2 is on a distinguished road
Hmm! well I can think of the following line :
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-27-2008, 08:13 PM
Senior Member
 
Join Date: Jun 2008
Posts: 481
masijade is on a distinguished road
interrupt and catch (InterruptedException)
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-27-2008, 08:20 PM
Member
 
Join Date: Aug 2008
Posts: 51
playwin2 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handlinh pdf open ,save,cancel in jsp sunkaras Advanced Java 1 05-18-2008 11:08 AM
How to cancel an individual timer in spite of canceling whole timer Java Tip java.util 0 04-04-2008 04:46 PM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 09:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 11:31 AM
Disable BACK and CANCEL buttons in wizard consutes SWT / JFace 2 11-12-2007 11:37 PM


All times are GMT +3. The time now is 08:41 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org