Cancelling scheduler tasks on plugin reload

Discussion in 'Plugin Development' started by Chlorek, Dec 22, 2012.

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

    Chlorek

    Hi, I found that when I reload server, all scheduled tasks by my plugin still works. I tried to create a table with schedules ID's, but it does not work with reload? What can I do to stop them on reload?
     
  2. Offline

    Gildan27

    This is what I do:

    Create a private variable for the task ID in the plugin:
    Code:
    private int taskId = 0;
    In onEnable():
    Code:
    taskId = getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
                public void run() { /* Whatever */ }
            }, 0L, interval * 20);
    In onDisable():
    Code:
    getServer().getScheduler().cancelTask(taskId);
    taskId = 0;
    If you have a table of task IDs, you use that instead of the private int, and just go through them with a for loop and cancel them.
     
  3. Offline

    Chlorek

    Oh fixed myself. Just onDisable() I added cancelAllTasks().
     
  4. Offline

    Bone008

    Don't use async tasks (especially when you don't know how to use them properly).

    Problem solved.

    (in a more detailed answer: because of the nature of threading, async tasks aren't stopped automatically when the plugin disables, sync tasks are because they are always being scheduled on the main thread)
     
  5. Offline

    Gildan27

  6. Offline

    Chlorek

    Bone008
    Sync tasks aren't stopped automatically too if I understand you right. Well I am using them.
     
  7. Offline

    Bone008

  8. Offline

    Chlorek

Thread Status:
Not open for further replies.

Share This Page