Reputation: 6912
I am writing an AsyncTask
as below:
class Load extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... aurl) {
//do job seconds
//stop at here, and does not run onPostExecute
}
@Override
protected void onPostExecute(String unused) {
super.onPostExecute(unused);
wait = false;
new Load().execute();
}
}
And the other method as below:
public void click() {
new Load().execute();
while(wait) {
;
}
}
The wait is a global boolean value.
Upvotes: 1
Views: 3627
Reputation: 1427
this is proppar way asynctask, you wont to use wait in any other method or sleep both are
use same normal method
private class AsyncTask extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
}
Upvotes: 3
Reputation: 36302
Your while(wait){}
loop is blocking the UI thread. onPostExecute
runs on the UI thread. Since you're blocking, onPostExecute
can never execute. Additionally you're creating a new instance of the Load
AsyncTask
and executing it in onPostExecute
, meaning you'll have an infinite loop of these unless you cancel at some point.
Upvotes: 2
Reputation: 20348
Looks like your doInBackground()
method is throwing an Exception
, try putting it in a try-catch
block.
Also I don't know why you are calling super.onPostExecute()
, try removing this too, it's not necessary to use this statement.
Upvotes: 2
Reputation: 234795
This code:
public void click() {
new Load().execute();
while(wait) {
;
}
}
will block the UI thread while the task executes. This is as bad as just running the background task in the foreground and should cause an Application Not Responding (ANR) error. Please don't do it.
Note that AsyncTask.onPostExecute()
will not be called if the task is cancelled. It also won't be called if doInBackground
throws an exception. Whatever is going on in doInBackground
may be causing this.
Upvotes: 5