public void waitForBuildToComplete(int max_wait) {
   RunListener.all().add(this);
   synchronized (this) {
     // check that the build is not already complete
     Run lastRun = this.job.getLastCompletedBuild();
     if (lastRun != null) {
       int lastNumber = lastRun.getNumber();
       if (lastNumber >= this.buildNumber) {
         return;
       }
     }
     try {
       this.wait(max_wait);
     } catch (InterruptedException ie) {
       return;
     }
   }
   this.unregister();
 }