0

I have an AsyncTask wich I call with

String a = ws.get();

So that process waits until asynctask is done.

But what I want is to wait for max 10 seconds, if asynch is not finished in that time I would like to go pass it and let it to its thing in the background.

So the flow would be:

  • call async
  • wait until its done but max 10s
  • other code

Any idea how I would accomplish this?

EDIT:

To furder explain what I am doing. I am printing some data on a mini bluetooth printer. When I press print I run a async task that prepares data to print. And this async task calls another async task to fetch some data from internet. But I need that data before I print so I use ws.get(). BUT if internet is slow and the task needs too much time (5-10seconds), I would like to bypass the ws.get() and print anyway BUT the async task still need to run since the data I get from internet will still go to my database it just will not get printed.

Tadej Vengust
  • 1,351
  • 4
  • 18
  • 35
  • You could do a `Thread.sleep(10000)` in your `doInBackground()` but then you would probably need to restart the task and not use `.get()`. But this is overall bad design. Making the user wait 10 seconds and stare at a blank screen is not good. If you explain *why* you would want to do this, maybe someone can help find a better way – codeMagic Mar 30 '15 at 14:11
  • The user doesent wait. Because I call that asynch in another asynch :D The point is that my app prints some data. I call printing in asynch. But in this printing I call another async to get some data from internet. But if this data isnt here in 10s or 5s I will print without the data but I still want the async to finish. – Tadej Vengust Mar 30 '15 at 14:14
  • Why do you want it to wait as when asynctask is running the main UI is not affected. – priyank Mar 30 '15 at 14:17
  • The point is that my app prints some data. I call printing in asynch. But in this printing I call another async to get some data from internet. But if this data isnt here in 10s or 5s I will print without the data but I still want the async to finish. – Tadej Vengust Mar 30 '15 at 14:17
  • use a timer task with interval as 1000 and run it until 10 secods(increment a variable inside timertask and exit it when variable reach 10) and the go for printing.Leaving the assync task. – Karthika PB Mar 30 '15 at 14:19
  • Do not use `AsyncTask`'s `get()` method - it runs in the UI thread – Ed Holloway-George Mar 30 '15 at 14:25
  • why even bother using an asynctask if you are using `get()` you are basically removing all the benefits of an asynctask – tyczj Mar 30 '15 at 14:47
  • @EdGeorge Check my edit as to why I use get() and why it is still in the background. – Tadej Vengust Mar 31 '15 at 06:04
  • @KarthikaPB Can you furder explain or give an example of how you would do that. Note that I dont want to exit async task just bypass it after some period of time – Tadej Vengust Mar 31 '15 at 06:48

0 Answers0