Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By akn_39

Thread: Wait for Asynchronous Task to finish

  1. #1
    akn_39 is offline Member
    Join Date
    May 2014
    Location
    Parksville, BC, Canada
    Posts
    9
    Rep Power
    0

    Default Wait for Asynchronous Task to finish

    Hello Everyone: What is the accepted way in Android/Java to have tasks wait for the completion of a previous task before executing? In my case I have 3 processes:

    1. Delete existing zip file from local storage (sdcard).
    2. Download new zip file and save in local storage (sdcard).
    3. Unzip new zip file.

    I have all these processes defined in Asynchronous Tasks, and they all work individually. But I am having difficulty structuring the code so that the Download will not run until the Delete has finished, and the Unzip will not run until the Download has finished.

    Any help appreciated.

    Thanks. . .

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Wait for Asynchronous Task to finish

    I can understand one thread/async task to allow other stuff to happen. But why three threads? Why not one thread which deletes, then downloads, then unzips?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Wait for Asynchronous Task to finish

    Well that, but even if that wouldn't be the bigger question, I'd also wonder how these "asynchronous tasks" work then. It sounds like they're actually just threads which sleep in a loop and periodically are allowed to wake up and do something.

    More details to understand the problem domain are sorely needed. Code of course, helps a lot.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    akn_39 is offline Member
    Join Date
    May 2014
    Location
    Parksville, BC, Canada
    Posts
    9
    Rep Power
    0

    Default Re: Wait for Asynchronous Task to finish

    Jim & gimbal2 - thank you both for responding to my question.

    Why 3 Asynchronous Tasks? The 3 processes (or at least the download and unzip) could all take some time, so I was using this technique for the events and also to be able to display an alert message (e.g. Downloading file - please wait...) while the processing is taking place. But you are right in the sense that although I have defined them as asynchronous, I don't want anything else to happen concurrently until each has completed. If there is a way to have the code execute sequentially (i.e. in a single thread) and display a separate "Please wait. . ." message while each is in progress, that would be a good solution. Here is sample code for the file delete:

    public void deletePreviousGTFSFile() {
    // Delete previous GTFS file

    final File file = new File("/data/unzip-files/gtfs-bc-transit.zip");

    AlertDialog.Builder builderDeletePrevious = new AlertDialog.Builder(
    this);
    builderDeletePrevious.setMessage(R.string.delete_w ait_message);
    final AlertDialog dlgDeletePrevious = builderDeletePrevious.create();
    dlgDeletePrevious.show();

    AsyncTask<Void, Void, Void> taskDeletePrevious = new AsyncTask<Void, Void, Void>() {

    @Override
    protected Void doInBackground(Void... params) {

    boolean deleted = file.delete();
    return null;
    }

    @Override
    protected void onPostExecute(Void result) {
    dlgDeletePrevious.dismiss();
    Toast.makeText(getApplicationContext(), "Delete complete...",
    Toast.LENGTH_LONG).show();
    super.onPostExecute(result);
    }
    };

    taskDeletePrevious.execute();
    };

    Also, the unzip process could take some time - in the emulator (Eclipse + Genymotion virtual device), it takes over 1 minute; I don't know how long it will take on a real device. If I still want to make this a separate thread, is an Asynchronous Task OK for something that takes this long? I have seen comments that it should not be used for anything longer than a couple of seconds.

    Regards. . .

    Tony N.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Wait for Asynchronous Task to finish

    Quote Originally Posted by akn_39 View Post
    I don't want anything else to happen concurrently until each has completed. If there is a way to have the code execute sequentially (i.e. in a single thread) and display a separate "Please wait. . ." message while each is in progress, that would be a good solution.
    You can leverage the publishProgress(...) and onProgressUpdate(...) methods and use a single AsyncTask.

    Or you can use three AsyncTasks where each of the first two starts the next in the sequence in its onPostExecute(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    akn_39 is offline Member
    Join Date
    May 2014
    Location
    Parksville, BC, Canada
    Posts
    9
    Rep Power
    0

    Default Re: Wait for Asynchronous Task to finish

    Darryl: Thank you for your suggestions - the nested onPostExecute/AsyncTasks sounds good. However, as others have pointed out, maybe AsyncTasks are not the best approach in this case. To restate what I am trying to do: I have the 3 processes (delete previous file, download new (zip) file, unzip new zip file) which need to run in sequence, and while each is running I would like to display a "Delete/Download/Unzip running - please wait..." message. If there is a "best practice" technique to accomplish this, I would be grateful for any assistance.

    Thanks. . .

    Tony N.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Wait for Asynchronous Task to finish

    What did you think publishProgress(...) and onProgressUpdate(...) are meant for? Of course, you would have to choose an appropriate generic type for the AsyncTask, definitely not <Void, Void, Void>.

    And please go through BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Wait for Asynchronous Task to finish

    Quote Originally Posted by DarrylBurke View Post
    What did you think publishProgress(...) and onProgressUpdate(...) are meant for? Of course, you would have to choose an appropriate generic type for the AsyncTask, definitely not <Void, Void, Void>.
    This.
    Especially since the code is already broken down into individual tasks, it would be a shame to then sellotape them together.

    Chaining tasks together like this is not unusual.
    Happens all the time with things like Akka, where the "I've finished" message from one task is used to prompt the launch of the next.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    akn_39 is offline Member
    Join Date
    May 2014
    Location
    Parksville, BC, Canada
    Posts
    9
    Rep Power
    0

    Default Re: Wait for Asynchronous Task to finish

    Darryl, Tolls: Thanks for your comments and suggestions. And thank you for the reference to formatting code in Posts - I felt sure this would exist, but didn't see it in the menu bar.

    Regards. . .

    Tony N.
    DarrylBurke likes this.

Similar Threads

  1. Wait for a file to finish writing before reading.
    By unaligned in forum New To Java
    Replies: 4
    Last Post: 06-30-2011, 09:46 AM
  2. gwt rpc asynchronous call
    By trust in forum Web Frameworks
    Replies: 1
    Last Post: 08-11-2009, 10:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •