0

I am trying to cancel a log run AsyncTask if a certain period of time exceeds (if AsyncTask is not automatically finised)

Below is the code where I setup my task to start with timeout

final ProfileDesc pdsc = new ProfileDesc();
    pdsc.execute();
    Thread th_pdsc = new Thread(){
        public void run()
        {
            try{
                pdsc.get(120000, TimeUnit.MILLISECONDS);
            }
            catch(Exception e){
                pdsc.cancel(true);
                ((Activity)context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                    Toast.makeText(context, "Download Time out. Please Try again later.", Toast.LENGTH_LONG).show();

                    }
                });
            }
        }
    };
    th_pdsc.start();

below is the code for my AsynTask

private class ProfileDesc extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
        dialogue = new ProgressDialog(MainActivity.this);
        dialogue.setTitle("Processing");
        dialogue.setMessage("Getting Header Information");
        dialogue.setIndeterminate(true);
        dialogue.setCancelable(false);
        dialogue.show();
    }
protected void onPostExecute(Void params) {
        super.onPostExecute(params);
            dialogue.dismiss();
}
protected Void doInBackground(Void... arg0) {
//long run work
return null;
}
}

After two minutes it's still running. How can I set up the time out? Note: I have followed this link of Stack Overflow for this code.

halfer
  • 19,824
  • 17
  • 99
  • 186
Saty
  • 2,563
  • 3
  • 37
  • 88

2 Answers2

0

What you had

 ProfileDesc pdsc = new ProfileDesc();
 pdsc.execute();

was enough. There is no need for a Thread. doInBackground is invoked on the background thread. So you can do you Network operations in doInbackgorund.

Secondly calling get makes AsyncTask no more Asynchronous as it blocks the ui thread

get(long timeout, TimeUnit unit) Waits if necessary for at most the given time for the computation to complete, and then retrieves its result

I guess you have misunderstood the use of Asynctask. Read the docs

http://developer.android.com/reference/android/os/AsyncTask.html

You may want to check this

Stop AsyncTask doInBackground method

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I just want to make sure that AsyncTask times out after a particular time period if it does not automatically call the postexecute as there are log run jobs in my doinbackground. The thread is to time out the AsyncTask not for the job...Am I doing wrong? what if AsyncTask never finish its doinbackground and user see a processdialogue throughout. – Saty Mar 26 '14 at 04:23
  • @Saty say you are doing a network operation. if doInbackground finishes its execution then control goes to onPostExecute. if not there will be a crash a exception thrown. TimeOut for Asynctask!. read the docs. `doInbackground` is invoked on the background thread. So why the other thread. Makes no sense. – Raghunandan Mar 26 '14 at 04:24
  • @Saty And AsyncTask is used for typically running operations for a few seconds. If its for a very long running operation use Intent Service/ Quoting docs **AsyncTasks should ideally be used for short operations (a few seconds at the most.)** – Raghunandan Mar 26 '14 at 04:26
  • okay so there are no way out to set timeout for AsyncTask? could you please let me know or give some example of Intent Service doing network operation with showing user some progress indicator. – Saty Mar 26 '14 at 04:31
  • @Saty a different problem post a different question – Raghunandan Mar 26 '14 at 04:31
0

Here new thread is not needed.Just remove "dialogue.setIndeterminate(true)". It means the "loading amount" is not measured.I think it's creating the problem.

Sanawaj
  • 384
  • 1
  • 6
  • okay...then how do AsyncTask know when to finish.... if the asynctask is not getting anything or internetconnection is disconnected in between? – Saty Mar 26 '14 at 05:12
  • @Saty in that case it will give you an exception which can be handled appropriately. You need to re-read the docs again – Raghunandan Mar 26 '14 at 06:03
  • So you mean to say I will surround all the code of doinBackground in the try block and check for the exception, if any exception happens like IOException while reading inputstream and alert the user appropriately? however I just checked the example which I did and it seems to work and time out the AsyncTask after 2 minutes – Saty Mar 26 '14 at 07:02