0

I need some help as i am just calling a method in a thread. now what i want is wait for reply form getData() method only for 15 seconds. If it reply before 15 seconds it should be terminated otherwise after 15 seconds it should be terminated. The code is given below:

boolean networkStatus;

private Runnable runnable; 

private ProgressDialog m_ProgressDialog = null;

        private Runnable returnRes = new Runnable() {

        @Override

        public void run() {

         if(networkStatus){

          setData();

         m_ProgressDialog.dismiss();

        }

    };

private void callGetdata(){

 runnable = new Runnable() {

        @Override

        public void run() { 

      networkStatus = getData();

             runOnUiThread(returnRes); 

        }

    };

    Thread thread = new Thread(null, runnable, "MovetoBackground");

    thread.start();

    m_ProgressDialog = ProgressDialog.show(this, "", getString(R.string.loadMsg), true);

}
2
  • 1
    I assume that getData is a blocking call... so unless you have access to that method, then there is nothing you can really do to terminate that call (except disconnect the underlying network connection). So what do you have in getData?
    – Kiril
    Jan 7, 2011 at 15:50
  • @Lirik or call Thread.stop() but thats a baaaad idea Jan 7, 2011 at 16:13

1 Answer 1

1

What does getData() actually do, since some network classes have timeout options builtin, such as the ServerSocket.setSoTimeout() function.
Also, I would suggest using Android's AsyncTask class, as it makes multithreading easier.

3
  • What you're doing there is crying out for AsyncTask (it event handles the progress dialog). As neuropie noted, look into that. Here's a starting point: developer.android.com/resources/articles/…. Jan 7, 2011 at 16:11
  • Actually we are using WCF services. So i am using ksoap2 library for work. But sometimes it takes a long time to response and the spinner goes continue. I just want to limit it for 15 sec. and in ksoap2 i have not found any connection timeout function. Jan 7, 2011 at 16:20
  • I haven't used ksoap, but this page may be useful (It uses a TimerTask to cancel the request). ksoap2.sourceforge.net/doc/api/org/ksoap2/transport/…
    – neuropie
    Jan 7, 2011 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.