2011-08-01 5 views
0

GWT가 실제로 단일 스레드라는 것을 알고 있지만 사용자 지연 명령을 사용하여 멀티 스레드 효과를 에뮬레이트하려고합니다. isBusy 부울 변수에 의해 제어되는 텍스트 애니메이션을 구현하려고합니다. 서버가 이미 응답했을 때 텍스트 조작 (애니메이션 효과)이 isBusy가 false 값으로 실행됩니다. 그러나이 코드는 while 루프에 붙어있는 것처럼 보입니다.GWT 멀티 스레드 효과

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; 
       } 
      }); 
     } 
    }); 

답변

3

스케줄러 적합한 도구,하지만 당신은 잘못된 명령을 사용하고 있습니다 : 여기

는 코드입니다. scheduleDeferred의 scheduleFixedPeriod insdead를 사용해야합니다. 다음은 희망을 갖고 도움을주는 작은 예입니다.

@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); 
} 
+0

그것은 I는 RPC 서비스에서 설정 한 정적 부울 변수에 실행()의 반환 값을 매핑 그러나 열심히 onSuccess() 및 onFailure() 동안; 실행이 멈추지 않는 이유가 궁금합니다. – xybrek

+0

내 경우에 작동합니다. – Stefan