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?
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:
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);