Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

AsyncTask

Comments in 'Plugin Development' started by xiaoq, Jun 20, 2015.

  1. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    I read many similar threads about Async task, but I can't understand what is Async Task? How does Async task differently to the normal task? And how to use Async task
    Legoboy0215 and RekkuzaRage like this.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    AsyncTask is basically a separate thread. It allows you to operate CPU-consuming code without blocking the main thread. However, the trouble of using AsyncTask is that you cannot get the data you get (if any) from the AsyncTask immediately. Instead, you have to execute those code in the onCompletion method of the AsyncTask.

    Keep in mind that although AsyncTask is not a thread itself, it is executed in a separate thread. You can't share any objects (except Threaded objects, but you don't need to understand them) between two threads. Not even arrays. You can only pass primitive data types like strings and numbers and booleans to a thread. Therefore, you cannot call Server::getInstance() or any PocketMine object-oriented API methods from AsyncTask onRun method.
    iksaku and xiaoq like this.
  3. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    So how to get/set/save config in AsyncTask?
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't do that at all. Don't interact with PocketMine in any ways in an AsyncTask. You can, but unless you understand threading very well, don't.

    Pass the value to your AsyncTask constructor. Serialize it. Store it in a field
    Unserialize it to use in onRun.
    iksaku likes this.
  5. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    Ok, but how...
  6. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    PHP:
      public function onCompletion(Server $server){

                
    $data $this->getResult();

                
    $players $data["player"];
            }
    I want to store $players to a place so Main.php can read it...
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why don't you use it in onCompletion directly?
    In that way, simply pass your main class into the AsyncTask. Don't use it in onRun, but use it in onCompletion.
  8. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    Wait.. but I already put this in on completion
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I meant that you can pass your main into the AsyncTask's constructor. And use it in onCompletion. As long as you don't use it in onRun, you can pass objects like in normal classes
  10. Guillaume351
    Offline

    Guillaume351 Active Member Plugin Developer

    Joined:
    Oct 11, 2013
    Posts:
    150
    Plugins:
    1
    Minecraft User:
    Guillaume351
    When you do in onCompletion, does it still run in an other thread ?
    Thanks
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No, the main thread.
    Guillaume351 likes this.

Share This Page

Advertisement