1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Cancel a task on command

Discussion in 'Development' started by ItsInspire, Jan 26, 2021.

  1. ItsInspire

    ItsInspire Silverfish

    Messages:
    15
    GitHub:
    itsinspire
    How do I cancel a task when I do a command? For example run this command:

    PHP:
    $task = new DiscoTask($this$sender->getName());
    $this->getScheduler()->cancelTask($task->getTaskId());
    It does nothing. Any idea how to get it working?
     
  2. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    $task->cancel()
     
  3. ItsInspire

    ItsInspire Silverfish

    Messages:
    15
    GitHub:
    itsinspire
    [Server thread/CRITICAL]: Error: "Call to undefined method TestPlugin\DiscoTask::cancel()"
    That doesn't work
     
  4. EndermannbugZJFC

    EndermannbugZJFC Slime

    Messages:
    84
    GitHub:
    endermanbugzjfc
    PHP:
    $task->getHandler()->cancel()
     
  5. ItsInspire

    ItsInspire Silverfish

    Messages:
    15
    GitHub:
    itsinspire
    That doesn't work...
     
  6. NutXzG

    NutXzG Baby Zombie

    Messages:
    132
    GitHub:
    NutXzG
    I hope that work
     
  7. ItsInspire

    ItsInspire Silverfish

    Messages:
    15
    GitHub:
    itsinspire
    That also doesn't work, No crash but it does nothing.
     
  8. xLeakDev Enzo

    xLeakDev Enzo Spider Jockey

    Messages:
    38
    GitHub:
    nya-enzo
    What you did here is schedule the task a second time, and then cancel this second one. What you would have to do is save the task id within an array on schedule of the task, then cancel the task with id of the one in array
     
  9. xLeakDev Enzo

    xLeakDev Enzo Spider Jockey

    Messages:
    38
    GitHub:
    nya-enzo
    PHP:
    public $taskIds = [];

    /* Schedule your task here*/
    $this->taskIds["task"] = $task->getTaskId();

    /* You want to cancel your task*/
    $this->getScheduler()->cancelTask($this->taskIds["task"]);
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.