-1

Does the asynchronous operation of an AsyncTask run in DoInBackground or OnPostExecute?

public class Task1 : AsyncTask
{
    protected override void OnPreExecute()
    {
    }

    protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
    {
        return true;
    }

    protected override void OnPostExecute(Java.Lang.Object result)
    {
    }
}

Update

What I think I meant to ask was if the routine/procedure/task that runs in DoInBackground is necessarily a thread, or is it more of an abstract concept of a task and if so is it scheduled to run as such (i.e. can it also be configured to run on the main thread). Similarly, is the "continuation" that runs in OnPostExecute also considered a task, since it runs on the main thread, but can also be "scheduled" to run with other "task's" on the same thread (like a click handler or on a lifetime override like onResume)?

samus
  • 6,102
  • 6
  • 31
  • 69
  • it does in doInBackground, preExecute and postExecute does in UI Thread https://developer.android.com/reference/android/os/AsyncTask – Joaquín Dec 14 '18 at 16:58
  • @JoaquinAlvarez But `doInBackground` runs in a separate thread, off of the UI, so is that "asynchronous", or is it when `postExecute` is scheduled to run on the UI, along with say a click event handler? – samus Dec 14 '18 at 17:09
  • I think the async part is doInbackGround since its a separated thread, meawhile postexecute and preexecute uses UI Thread – Joaquín Dec 14 '18 at 17:10
  • @JoaquinAlvarez But isn't `doInbackGround` running "in parallel"? – samus Dec 14 '18 at 17:16
  • @JoaquinAlvarez https://blogs.msdn.microsoft.com/benwilli/2015/09/10/tasks-are-still-not-threads-and-async-is-not-parallel/#comment-4375 – samus Dec 14 '18 at 17:17
  • https://stackoverflow.com/questions/6133574/how-to-articulate-the-difference-between-asynchronous-and-parallel-programming check this answer – Joaquín Dec 14 '18 at 17:24

2 Answers2

1

asynchronous done in DoInBackground() and onPostExecute You get the result and its UI thread

Tariqul Islam
  • 680
  • 6
  • 14
  • `DoInBackground` runs in it's own thread, and `onPostExecute` back onto the UI/Main thread, correct? – samus Dec 14 '18 at 17:05
  • Yes. That`s why DoInBackground called background thread. And it not interrupt anything on main thread – Tariqul Islam Dec 14 '18 at 17:08
  • So `DoInBackground` is running "out-of-sync" technically, but more like "in parallel", to the UI thread. However `onPostExecute` is scheduled on the UI thread, along with whatever may be currently running, either before or after it, which is more like "out-of-order", correct? – samus Dec 14 '18 at 17:14
  • Yeah. Exactly . – Tariqul Islam Dec 14 '18 at 19:09
  • `onPostExecute` waiting for the result which is processing in `DoInBackground`. – Tariqul Islam Dec 14 '18 at 19:11
1

These are the 4 steps a task goes through when an asynchronous task is executed:

  1. onPreExecute(), invoked on the UI thread before the task is executed.
  2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing.
  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...).
  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes.

As per documentation doInBackground(Params...):

doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

Async Task | Android Developers

To answer your question, any background computation takes place in the doInBackground method.

G. Spyridakis
  • 431
  • 1
  • 10
  • 21
  • So would you say `doInBackground`, which runs within it's own thread concurrent to the UI thread, is the asynchronous operation, or would you say the "continuation" of it ran within `onPostExecute` is? – samus Dec 14 '18 at 17:35
  • Yes, exactly. Any background computation should be executed in the `doInBackground` method – G. Spyridakis Dec 14 '18 at 17:57
  • But how is `onPostExecute` ran on the same thread as say a click event that comes right before it? Are both of these "tasks" scheduled to run on the same thread by some task scheduler? – samus Dec 14 '18 at 18:00
  • `onPreExecute` and `onPostExecute` are invoked on the UI thread. Have also in mind that the task can be executed only once (an exception will be thrown if a second execution is attempted.). – G. Spyridakis Dec 14 '18 at 18:09