#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Java Thread problem


    public void actionPerformed(ActionEvent e) {
    ProgressBar.setIndeterminate(true);
    if(changeButton==0){
    Start.setEnabled(false);
    }else if(changeButton==1){
    Start2.setEnabled(false);
    }

    // Instances of javax.swing.SwingWorker are not reusuable, so
    // we create new instances as needed.
    task = new Task();
    task.addPropertyChangeListener(this);
    task.execute();
    }


    public void propertyChange(PropertyChangeEvent evt) {
    if ("progress" == evt.getPropertyName()) {
    int progress = (Integer) evt.getNewValue();
    ProgressBar.setIndeterminate(false);
    ProgressBar.setValue(progress);
    // Area.append(String.format("Completed %d%% of task.\n", progress));//මුදලක්
    //Area.append(String.format("මුදලක් %d%% of task.\n", progress));//මුදලක්
    }
    }



    class Task extends SwingWorker<Void, Void> {
    /*
    * Main task. Executed in background thread.
    */
    @Override
    public Void doInBackground() {








    ResultSet rsCountRow=con.getQuery("select nDepMFID from dep_deposit_master where nDepTypeID='"+DepTypeID[cmbDepType.getSelectedIndex()]+"'");

    try {
    while(rsCountRow.next()){
    max++;
    }
    } catch (SQLException ex) {
    ex.printStackTrace();
    }

    Vector vec = new Vector();
    ResultSet rsMFID=con.getQuery("select nDepMFID from dep_deposit_master where nDepTypeID='"+DepTypeID[cmbDepType.getSelectedIndex()]+"'");
    try {
    while(rsMFID.next()){
    count++;
    bak=max/100;
    MFID=rsMFID.getInt("nDepMFID");



    //Call SP


    if(count==bak){
    progress++;
    setProgress(Math.min(progress, 100));
    count=0;
    }
    }
    } catch (SQLException ex) {
    ex.printStackTrace();
    }


    Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR);
    setCursor(normalCursor);





    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    }

    return null;
    }

    /*
    * Executed in event dispatch thread
    */
    public void done() {
    Toolkit.getDefaultToolkit().beep();
    if(changeButton==0){
    Start.setEnabled(true);

    }else if(changeButton==1){
    Start2.setEnabled(true);
    }


    }

    }


    Hey....

    I used above methods to view a report with running Progress bar..
    Progress Bar will start when a button press.
    so after a progress is complete..when I press button again, thread will again run twice...

    How can I solve this problem...or how to kill thread or..give me a halp please....
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    how to kill thread
    Use a boolean variable to tell the code in the thread to exit.

    Please edit the post and wrap the code in code tags.

IMN logo majestic logo threadwatch logo seochat tools logo