How to cancel closure task?

Discussion in 'Development' started by Levi, Jun 9, 2020.

  1. Levi

    Levi Skeleton

    Messages:
    917
    GitHub:
    captainleviftw
    PHP:
    $core->getScheduler()->scheduleRepeatingTask(new ClosureTask(function (int $currentTick) use ($core): void {
                    
    $core->getScheduler()->cancelTask($currentTick);
                }), 
    20);
    I also tried $this->getTaskId() but it doesn't work
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    473
    Save the task handler in a variable and pass into the task as reference
     
    Muqsit and Primus like this.
  3. Levi

    Levi Skeleton

    Messages:
    917
    GitHub:
    captainleviftw
    so $task = core->getScheduled()...?
     
  4. wolfdale

    wolfdale Zombie Pigman

    Messages:
    473
    Yep and use(&$task,$core)
     
  5. Levi

    Levi Skeleton

    Messages:
    917
    GitHub:
    captainleviftw
    Do I cancel inside the brackets of task?
     
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    473
  7. Levi

    Levi Skeleton

    Messages:
    917
    GitHub:
    captainleviftw
    i cant
     
  8. wolfdale

    wolfdale Zombie Pigman

    Messages:
    473
    Show your code?
     
  9. Levi

    Levi Skeleton

    Messages:
    917
    GitHub:
    captainleviftw
    $task = $this->plugin->getScheduler()->scheduleRepeatingTask(new ClosureTask(function (int $currentTick) use ($task): void {
    $this->plugin->getScheduler()->cancelTask($currentTick);
    }), 20);
     
  10. wolfdale

    wolfdale Zombie Pigman

    Messages:
    473
    Just do $task->cancel(); ?
     
    Muqsit likes this.
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,572
    GitHub:
    muqsit
    Messy, may wanna write your own CancellableClosureTask
    PHP:
    $handler null;
    $handler $scheduler->scheduleRepeatingTask(new ClosureTask(function(int $currentTick) use(&$handler) : void{
        
    $handler->cancel();
    }), 
    1);
     

Share This Page

  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.