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

Run function in background

Discussion in 'Development' started by LucGamesDE, Mar 18, 2017.

  1. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I think the problem is that the function in the main class don't waits for the end of the AsyncTask. How can I return it if the AsyncTask is done?
    Otherwise the variable is always empty
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you wait, something like a onComplete($result) should do
     
  3. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I have this in my AsyncTask. There I can echo the result. But I want to display it on command (using a function)
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    call back a main plugin with a oncomplete there which you may continue displaying it, ex sending the cmd back to the player or smth

    since PMMP have none of these weird Promises dohickies which is suppose to get return on a pending task, you are probably wondering what that is, dont ask me maybe google can help but i never touched these stuff because pmmp have none of these, i only know they exist on some libs
     
  5. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I don't really understand... Can u give me an example? Or is there a better way to get the status of another server?
     
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    like on complete under async task completed call server->getplugin->yourownplugin->onComplete($results)
    if you dont get it, nether do i
     
    jasonwynn10 and 0x15f like this.
  7. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    But how can I get the result of onComplete in the main task?? I want to get the result of the starter task (getStatus() in the main)
     
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    private $ip$port$timeout$server$status;
    public function 
    __construct(Server $server$ip$port$timeout 10){
        
    $this->ip $ip;
        
    $this->port $port;
        
    $this->timeout $timeout;
        
    $this->server $server;
    }

    public function 
    onRun(){
        
    $minecraft = @fsockopen($this->ip$this->port$this->timeout);
        
    $this->status $minecraft == true "online" "offline";
    }

    public function 
    onCompletion(Server $server){
        
    $server->getLogger()->notice("
            Background task has finished. The output was:
        "
    .$this->status);
        
    $server->getPluginManager()->getPlugin("pluginName")->result $this->status;
    }
     
  9. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    No. I already tried that. And it doesn't work because when I run the command, the variable is empty. And if I do /info list, I see more than one server
     
  10. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    if you wont get it, i dont think you will ever get it from me
    something along the lines of jasonwynn10 example but using a function called onComplete (or anything you will) instate of a variable
     
  11. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    That's in my AsyncTask ...
    PHP:
    public function onCompletion(Server $server){
        
    $server->getLogger()->notice("
            Background task has finished. The output was:
        "
    .$this->status);
        
    $server->getPluginManager()->getPlugin("MyPlugin")->onComplete() = $this->status;
    }
    It works. But I don't get it in the main task. I could add onComplete($status). But I want to return it in the main task :/
     
  12. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    How does that even work?! You set a function equal to a variable! :facepalm:
     
    Last edited: Mar 21, 2017
    Sandertv likes this.
  13. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I think u really understand what I want to do.

    1. I start the AsyncTask in a function (getStatus(){}) in the main Class.
    2. The AsyncTask works correctly and gives the right result onCompletion(){}
    3. In the getStatus() task I want to get the result of the startet AsyncTask (doesn't work). I start the AsyncTask using this:
    PHP:
    $status $this->getServer()->getScheduler()->scheduleAsyncTask(new StatusTask($ip$port$timeout));

    //Now I want to return $this->getResult () from the task in this function
     
  14. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    $status $this->getServer()->getScheduler()->scheduleAsyncTask($task = new StatusTask($ip$port$timeout)); // status is null
    $result $task->getResult();
     
  15. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    This gives me nothing. And no error
     
  16. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Jack Noordhuis likes this.
  17. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I got it to a class in the main class. But how can I display it now on Command?
    PHP:
    public function asyncTaskCallBack($data) {
    echo 
    $data;
    }
     
  18. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you cant, you just have to display it to the player on the callback
     
  19. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    It's not only on command. I run the function to get the status on command and to refresh status signs. How would u do that?
     
  20. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you can only do it AFTER command, after the callback query have resolved
    there you just need to tell player and update sign inside the callback/completed function
     
  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.