Solution 1 :
I’m not sure I understand your question fully but if I understand correctly but here is what I would do. Move your do_action();
call to after progress_dialog.show();
then call progress_dialog.dismiss();
from within the do_action();
method (at the end once your desired code has executed). Also note that the progress dialog is deprecated and you should use something like the progress bar instead. If I misunderstood your question let me know and ill try to give you a answer.
Solution 2 :
Well, I know a way to full fill your requirement which name is the async task.
Async task?
AsyncTask was intended to enable proper and easy use of the UI thread.
However, the most common use case was for integrating into UI, and
that would cause Context leaks, missed callbacks, or crashes on
configuration changes. It also has inconsistent behavior on different
versions of the platform, swallows exceptions from doInBackground, and
does not provide much utility over using Executors directly.
In your case, I think the do_action() is doing a background task. If so you could use the async task like this.
public void onButtonClick(View view) throws InterruptedException {
// Execute your task
new ButtonClickTask().execute();
}
private class ButtonClickTask extends AsyncTask<Void, Void, Void> {
// This task run on UI thread
// Before starting the background task show the progress dialog.
protected void onPreExecute () {
progress_dialog.show();
}
// This task run on background thread, if you do any UI action from this function it will terminate your application and give an error.
protected Long doInBackground(Void... params) {
do_action();
return null;
}
// Once your background task is completed. This method will be called.
protected void onPostExecute(Void param) {
// Your do_action() task is completed
// Now you could dismiss your progress_dialog here and do other tasks
progress_dialog.dismiss();
}
}
Problem :
My program runs a function upon a button press. Inside, I perform an action and then show a progress dialog spinner. Now I would like to wait until I get a “response”, so I acquire a semaphore for T amount of time.
I thought that the function will execute and a progress bar will show up before I try to acquire the semaphore, but instead the program ends up in the semaphore acquire and sleeps, blocking the other functions(probably). The progress dialog shows up AFTER the semaphore times out. How do I ensure that all previous functions completed successfully before proceeding? Here is my onClick function:
public void onButtonClick(View view) throws InterruptedException {
do_action();
progress_dialog.show();
boolean acquired = my_semaphore(15, TimeUnit.SECONDS);
if (!acquired) {
// action timed out
// do something
} else {
// set elsewhere in the program
// a - ok
}
}
How can I implement it in a better way?
Comments
Comment posted by rookie
To clarify…do_action and progress dialog do no fully complete execution when I insert the semaphore get call. Without the get call, both functions execute and I see the progress dialog. When I add the semaphore get, the previous two functions ie do_action and progress bar show do not complete, I also dont see the progress bar until the semaphore times out.
Comment posted by rookie
I tried this method earlier but I got a runtime exception, dont recall the error, something to do with Looper. Anyways, this method doesn’t have the semaphore get and that is the crucial part. When I perform do_action(), I am expecting a response in a different part of the program which will set the semaphore. If the response doesn’t arrive within the timeout, I will perform a cleanup.