Show ProgressDialog and call OnBackPressed() after certain period

bigb055

New Member
#1
The ProgressDialog should display for 5 secs after which OnBackPressed() should execute. Currently the ProgressDialog never appears on screen. If I put OnBackPressed() inside the delegate progress dialog is displayed but then I get error that OnBackPressed() should be executed from MainThread. What can I do?
Mã:
private async SomeAsyncMethod()
{
   await ShowProgressDialog();
   ......
}

private Task ShowMessage()
        {
            var progressDialog = ProgressDialog.Show(this, "Bestandskorrektur", "Bestandskorrektur wird ausgeführt! Bite warten...", true);
            new Thread(new ThreadStart(delegate
            {
                Thread.Sleep(5000);
                RunOnUiThread(() => progressDialog.Hide());
                OnBackPressed(); // Gives error.
            })).Start();

            return Task.CompletedTask;
        }
UPDATE: Solution based on answers:
Mã:
var pd = ProgressDialog.Show(this, "Bestandskorrektur", "Bestandskorrektur wird ausgeführt! Bitte warten...", true);
                var h = new Handler();
                void action()
                {
                    RunOnUiThread(() => pd.Hide());
                    OnBackPressed();
                }
                h.PostDelayed(action, 5000);
 

Admin

Administrator
Thành viên BQT
#2
Try using a Handler().postDelayed() like below -
Mã:
new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //hide progressbar
                        onBackPressed();
                    }
                }, 5000);
 
Top