-1

Wait or Sleep...which method will be suitable?

void A()
{
    //Some Code 1

    some_function(); // calling a method and will seek for a response from other device, communicating on bluetooth
    //Some code 2
}

I want my program to wait for the process of some_function(). However at the moment it starts executing "Some Code 2" part of function A().

And kindly if someone specify wait method implementation my case.

p.s: Async is not my priority.

Muhammad Abdullah
  • 271
  • 1
  • 2
  • 15

1 Answers1

1

Since you dint want to do async task,making the thread sleep may freeze the UI. Even if it didn't, it is still insanely bad practice.

so first thing - you have to do it on a separate thread. and Asynctask is best for it.

some_function() would be executed before //Some code 2 if it is not running on a separate thread(As you said its not Asynctask).But in case it is running on a separate thread(which it should be)-.

you can just simply call "//Some code 2" inside some_function() at the end.

OR

if you do not want to do that,try it like this-

First make an interface -

public static interface On_some_function_complete{

    void onComplete(what ever parameters you want);

}

put an instance of this interface in your some_function() as parameter where ever you define it and call the onComplete method in the end of the function ,like-

void some_function(On_some_function_complete arg_on_complere){

   //what ever stuff your code dose

  arg_on_complere.onComplete(/*with required arguments*/);
}

now make call like -

void A()
{
   //Some Code 1

   some_function(new On_some_function_complete{

                @Override
                public void onComplete(Bitmap result) {
                   //Some code 2
                }
            }); 

}

OR

you must have some function that you can override to handle on-Compete action

Flying Monkey
  • 669
  • 1
  • 5
  • 13