6

I'm trying to use Thread in my project to send emails. When I click on a Button, a Thread is started and a ProgressBar is displayed. As soon as all mails are sent, the ProgressBar doesn't disappear.

This is my code:

Button btnSendMail = new Button("Mail");
btnSendMail.addClickListener(this);
@Override
public void buttonClick(ClickEvent event) {     
    if(event.getButton() == btnSendMail){   
            sendMail();
    }
}
}    

private void sendMail(){
     List<String> list = new ArrayList<String>();
     list.add("mymail@domain.com");
     list.add("metoyou@domain.com");
     list.add("thisismymail@domain.com");

     new Thread(){
         public void run(){
             while(!isInterrupt()){
                 progressbar.setVisible(true);
                 for(String send : list){
                     new SendMailClass(send); //javamail class
                 }           
                 progressbar.setVisible(false);
                 interrupt();
    }   
}.start();


}

How can I control visibility of the ProgressBar from a separated Thread?

aboger
  • 2,214
  • 6
  • 33
  • 47
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118
  • 1
    Remember, Vaadin is basically GWT, which is part Java code running on your server and part JavaScript running in your browser. The code you've written, while perfectly valid (syntactically), does not compile down to JavaScript that can run in your browser. Can you post the code where you create the progress bar? – J Steven Perry Feb 06 '14 at 14:58

1 Answers1

7

To update UI elements from a background thread, you have to activate either push or polling.

The documentation can be found in the vaadin book.

https://vaadin.com/de/book/vaadin7/-/page/advanced.push.html

In addition to enabling push, you also need to synchronize access to the UI elements as described in section "11.16.3. Accessing UI from Another Thread"

André Schild
  • 4,592
  • 5
  • 28
  • 42
  • wow dude, thank you...now works. I activated @Push in my UI and used push() to manual mode UI.getCurrent().push() and works ! :D ..So nice man, rocks. Thanks again ! – FernandoPaiva Feb 06 '14 at 16:07
  • now my Thread returned this error: "Exception in thread "Thread-15" java.lang.IllegalStateException: A connector should not be marked as dirty while a response is being written." ... Any idea ? – FernandoPaiva Feb 06 '14 at 18:01
  • How did you solve it? (It might be interesting for others which run into the same problem) – André Schild Feb 06 '14 at 19:40
  • 1
    well, the problem occurred when I added progressbar.setVisible(true) before while(!isInterrupted()) in run() method. – FernandoPaiva Feb 07 '14 at 09:40
  • @AndréSchild Vaadin tells "Making changes to a UI object from another thread and pushing them to the browser requires locking the user session when accessing the UI. Otherwise, the UI update done from another thread could conflict with a regular event-driven update and cause either data corruption or deadlocks. Because of this, you may only access an UI using the access() method, which locks the session to prevent conflicts. It takes a Runnable which it executes as its parameter." https://vaadin.com/docs/-/part/framework/advanced/advanced-push.html#advanced.push.running – shinchillahh Jun 20 '16 at 09:01