Stopping a repeating task

Discussion in 'Plugin Development' started by phimt, Jun 16, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    phimt

    Whats the best way to stop a repeating Sync task? Is it possible to stop a task within the runnable? I have a repeating task that checks a few blocks for certain materials, and should stop when the check is true, as there is no need for it afterward.

    Thanks
     
  2. You can stop it even inside the runnable. If you have only this task, it's easy to do.
    Code:
    Bukkit.getScheduler().cancelTasks(<MainClassInstance>);
    Otherwise you'd need to save the id that you get returned when creating a task. Using that id you can cancel the task.
    Code:
    int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(..);
    Bukkit.getScheduler().cancelTask(taskID);
     
  3. Offline

    NuclearW

    Alternatively don't make it a repeating task, but rather a normal task that will re-register itself if it needs to be repeated.
     
  4. Offline

    phimt

    Thanks, I got it to work :D.
     
  5. Offline

    Eistee²

    With wich way did you do that :D?
    Can you may post it?

    Mfg Eistee
     
  6. Offline

    Everdras

    One way to do it is to pass the Scheduler object to the constructor of the Runnable, and have the Runnable register itself and save the ID returned.,

    Then, in the Runnable's run() method, determine if you have reached the stopping case, an if you have, use the Scheduler to cancel the task by its ID.
     
  7. Offline

    phimt

    I had a method that sets up a delayed task call itself if the condition was not met
     
Thread Status:
Not open for further replies.

Share This Page