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

How to cancel closure task?

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

  1. Levi

    Levi Skeleton

    Messages:
    955
    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:
    535
    GitHub:
    diamond-gold
    Save the task handler in a variable and pass into the task as reference
     
    Muqsit and Primus like this.
  3. Levi

    Levi Skeleton

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

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Yep and use(&$task,$core)
     
  5. Levi

    Levi Skeleton

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

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    i cant
     
  8. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Show your code?
     
  9. Levi

    Levi Skeleton

    Messages:
    955
    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:
    535
    GitHub:
    diamond-gold
    Just do $task->cancel(); ?
     
    Muqsit likes this.
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    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);
     
  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.