Using Thread with Vaadin?

谁说我不能喝 提交于 2020-01-10 03:57:32

问题


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?


回答1:


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"



来源:https://stackoverflow.com/questions/21603492/using-thread-with-vaadin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!