GWT Multi-threaded effect
I know GWT is single threaded by nature, however I am trying to user deferred command to emulate a multi-threaded effect. I am trying to implement a text animation which is controlled by the isBusy boolean variable, which the text manipulation (animation effect) runs unti isBusy gets false value, when the server responded already. However this code seems to get stuck with the while-loop?
Here is the code:
loginButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
isBusy = true; // boolean
// Show animating effect...
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
while(isBusy == true) {
for (int i=0;i<3;i++) {
if (i==0) { loginButton.setText("."); }
if (i==1) { loginButton.setText(".."); }
if (i==2) { loginButton.setText("..."); }
开发者_开发百科 }
}
}
});
loginService.getUser(usernameBox.getText(), passwordBox.getText(), new AsyncCallback<User>() {
@Override
public void onSuccess(User result) {
isBusy = false;
}
@Override
public void onFailure(Throwable caught) {
isBusy = false;
}
});
}
});
Scheduler was the right tool, but you are using the wrong command. You have to use scheduleFixedPeriod insdead of scheduleDeferred. Here is a small example which showes hopefully helps you.
@Override
public void onModuleLoad() {
final Label l = new Label("");
RootPanel.get().add(l);
Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {
@Override
public boolean execute() {
int i = l.getText().length();
if (i == 0) {
l.setText(".");
}
else if (i == 1) {
l.setText("..");
}
else if (i == 2) {
l.setText("...");
}
else if (i == 3) {
l.setText("");
}
return true; //as long as it returns true the execute is repealed after XX milliseconds
}
}, 200);
}
精彩评论