0

I have time consuming function upload /* around 10 sec */. I am trying before start upload to show ProgressDialog and when upload finishes to dismiss ProgressDialog and I trying like

        pd = ProgressDialog.show(Uploader.this,
                "Connecting...", "Uploading",
                true, false);
        Uploader.this
        .runOnUiThread(new Runnable() {
            public void run() {

                upload();
                handler.sendEmptyMessage(0);

            }
        });

Problem is that Progress doesn't show for around 10s, only flashes, like it first upload and then call show and dismiss ( I tried to put show above thread, but it is the same ). What to do ? ( Upload is void function)

Damir
  • 54,277
  • 94
  • 246
  • 365

2 Answers2

3

You're upload code should not run on the UI thread. If it does, then it will block that thread from doing anything else, such as displaying a progress dialog. Save your progress dialog, show it, spawn a new thread to do the upload, and when the upload is complete, use a Handler to get back to the UI thread and close the progress dialog

Micah Hainline
  • 14,367
  • 9
  • 52
  • 85
  • +1 - agree. @Damir You should use `AsyncTask` for painless threading. – Adil Soomro Oct 29 '11 at 12:26
  • @Adil Soomro Can you help me with AsyncTask, what to put in <> when update is void and no need parameters? – Damir Oct 29 '11 at 12:29
  • use `AsynTask` and have a look [here](http://stackoverflow.com/questions/2702695/android-async-task-progressdialog-isnt-showing-until-background-thread-finishes) – Adil Soomro Oct 29 '11 at 12:39
  • I'm using an `AsyncTask` and on `onPreExecute` I create a `ProgressDialog` and on `onPostExecute` I dismiss it. But I have the same problem too. What could be wrong? The only thing is that in `doInBackground` override, I connect to an HTTP service. – Saeed Neamati Aug 23 '15 at 11:33
1

Why do you perform upload operation, instead of another thread, on UI thread, which is currently trying to execute progress dialog?

Gökhan Barış Aker
  • 4,445
  • 5
  • 25
  • 35