1

I am using asynctask to execute some task. I also want to implement to complete in 60 sec else give a time out exception message.

So I am using the AsyncTask.get(time,timeFormat);

Example:

new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        validateConnection.execute().get(60, TimeUnit.SECONDS);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    } catch (TimeoutException e) {
                        stopConnTask();
                        invalidCrediantialsError(Utilities.TIMED_OUT_ERROR);
                        e.printStackTrace();
                    }catch(CancellationException e){
                        e.printStackTrace();
                    };
                }
            }).start();

It works fine as AsyncTask. Get blocks in UI thread so I am running it in separate thread. Is this approach right or I have to think of something else.

Hybrid Developer
  • 2,320
  • 1
  • 34
  • 55
Bora
  • 1,933
  • 3
  • 28
  • 54

2 Answers2

1
public final Result get ()

Added in API level 3
Waits if necessary for the computation to complete, and then retrieves its result.

Returns
The computed result.
Throws
CancellationException   If the computation was cancelled.
ExecutionException  If the computation threw an exception.
InterruptedException    If the current thread was interrupted while waiting.

Calling get() will not make asynctask asunchronous. get() waits for the result blocking the ui thread. Remove get() and use execute like for example new TheTask().execute().

Also asynctask must be invoked on the ui thread

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • It run fine , i also do not want to use get, actually i want my asynctask to complete before 60 sec else give a time out message. Thats why i used get. – Bora Oct 10 '13 at 07:31
  • Actually i am usign asynctask to check a connection string for connecting to database, weather the connection string is returning connection or it is wrong – Bora Oct 10 '13 at 07:32
  • what is that you are doing in asynctask a http request? – Raghunandan Oct 10 '13 at 07:32
  • No buddy, using to check connection string for database – Bora Oct 10 '13 at 07:44
  • to check conenction what do you do?. You check n/w availability or internet connectivity. – Raghunandan Oct 10 '13 at 08:47
  • No database connection string like . jdbc:jtds:sqlserver://127.0.0.1:1433/Blog", "user", "password") – Bora Oct 10 '13 at 09:33
1

An AsyncTask is primarly intended for longer running tasks that would cause an ANR in the UI thread and report back its results to the user interface.

If your task runs up to 60 sconds, this means your activity should stay open the same time. Do you really want this? Or don't you need to apply any results to the UI?

In any case, I'd recommend to use a service having a thread inside. You could either start an intent from that service or send a broadcast for further processing by the user interface.

p.s. this post gives some idea in which case AsyncTask.get(...) might make sense: basically said, only if your AsyncTask is doing some initialization that is a kind of fundament for the for the UI.

p.p.s: have you considered to specify a JDBC connection timeout? Check for instance this site for more details.

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • I am trying to check a mysql db connection string using the asynctask. If the string goes correct then the result may come with a sec. However user may enter all incorrect credentials, so overall i want the check should be till 60 sec else give time out exception – Bora Oct 10 '13 at 07:41
  • Any time the async task finishes or time out exception come i need to open a dialogfragment for next step . – Bora Oct 10 '13 at 07:44
  • I want the task to finish with in 60 sec because usually db connections are fast and if a task is taking more then 60 sec there fore some thing is wrong in user credentials for connecting db. – Bora Oct 10 '13 at 07:48
  • @Suresh Bora: if you really need to block your whole UI until you are sure that the database connection is working, then `get(...)` might be a good choice. However, for achieving a stable network/database operation in various environments I'd rather stick to a service that tries to connect. May be including a kind of queue implementation. – Trinimon Oct 10 '13 at 12:03