How it happen? I have create a thread to wait before it can dismiss, but what it did, it show and dismiss right after it start but the While Loop keep updating UI without ProgressDialog. And if I commented on Dismiss, it show properly but it will not close and keep spinning. I run this on emulator.
private ProgressDialog progress;
Below are what I have tried:-
Normal
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); progress = new ProgressDialog(this); progress.Indeterminate = true; progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner); progress.SetMessage("Loading... Please wait..."); progress.SetCancelable(false); progress.Show(); int count = 0; while (count < 10) { count += 1; Toast.MakeText(this, "Running count: " + count, ToastLength.Long).Show(); Thread.Sleep(500); } Toast.MakeText(this, "Thread complete running", ToastLength.Long).Show(); progress.Dismiss(); }
Another try:-
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
ThreadPool.QueueUserWorkItem(o => RunLoadThread());
}
void RunLoadThread()
{
RunOnUiThread(() => {
progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
progress.SetMessage("Loading... Please wait...");
progress.SetCancelable(false);
progress.Show();
});
new System.Threading.Thread(new System.Threading.ThreadStart(() => {
int count = 0;
while (count < 10)
{
count += 1;
RunOnUiThread(() => { Toast.MakeText(this, "Running count: " + count, ToastLength.Long).Show(); });
Thread.Sleep(2000);
}
Thread.Sleep(1000);
RunOnUiThread(() => { Toast.MakeText(this, "Running counting stars", ToastLength.Long).Show(); });
})).Start();
}
Another also:-
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
ThreadPool.QueueUserWorkItem(o => RunLoadThread());
}
void RunLoadThread()
{
RunOnUiThread(() => {
progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
progress.SetMessage("Loading... Please wait...");
progress.SetCancelable(false);
progress.Show();
});
new System.Threading.Thread(new System.Threading.ThreadStart(() => {
int count = 0;
await Task.Run(() =>
{
while (count < 10)
{
count += 1;
RunOnUiThread(() => { Toast.MakeText(this, "Running count: " + count, ToastLength.Long).Show(); });
Thread.Sleep(500);
if (count == 10)
{
Thread.Sleep(1000);
}
}
return true;
});
RunOnUiThread(() => { progress.SetMessage("Complete Running."); });
progress.Dismiss();
RunOnUiThread(() => { Toast.MakeText(this, "Running counting stars", ToastLength.Long).Show(); });
})).Start();
}
Answers
Hi,
You need to work in this way like,
Override these method OnPostExecute , OnPreExecute and OnProgressUpdate.
hi @Ahsan_Siddique , i will revert back to you on Monday after I applied it on office hour
How to override? Do I need to create new activity? Because error appear when I insert the override method to MainActivity.cs class
Check it.

Ok, I see the different, mine use the
public class MainActivity : Activity
and yours usingpublic class MainActivity : AppCompatActivity
But when I retype to change the Activity to AppCompatActivity, no suggestion comes out from Intellisense. I'm using Blank App. Should I go for Cross Platform? I start on Blank App as I was just a week way on Phone Application. I used to Windows Application. A bit challenge when move to phone application
Bro you need to create a new Task for your logic.
Appcompat is just a design library that's used for layout designing not used for backend code.
Create a Task on your button click operation and in this task override these method write your logic for OnPreExecute , PostExecute etc.
If you want to use appcompat for your layout then you to install AppCompat package from Nuget package manager.