Forum Xamarin.Android

ProgressDialog dismiss just after show

luieyluiey Member ✭✭

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();
}

Tagged:

Answers

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    Hi,
    You need to work in this way like,

    `private ProgressDialog mDialog = new ProgressDialog(Application.Context);
    protected override void OnPreExecute()
            {
                mDialog.Window.SetType(Android.Views.WindowManagerTypes.SystemAlert);
                mDialog.Show();
            }
    
            protected override void OnProgressUpdate(params string[] values)
            {
                mDialog.SetMessage(values[0]);
            }
            protected override string RunInBackground(params Stream[] @params)
            {
                //Write your logic here
            }
            protected override void OnPostExecute(string result)
            {
               mDialog.Dismiss();
               //Show your result here
                }
            }`
    

    Override these method OnPostExecute , OnPreExecute and OnProgressUpdate.

  • luieyluiey Member ✭✭

    @Ahsan_Siddique said:
    Hi,
    You need to work in this way like,

    `private ProgressDialog mDialog = new ProgressDialog(Application.Context);
    protected override void OnPreExecute()
            {
                mDialog.Window.SetType(Android.Views.WindowManagerTypes.SystemAlert);
                mDialog.Show();
            }
    
            protected override void OnProgressUpdate(params string[] values)
            {
                mDialog.SetMessage(values[0]);
            }
            protected override string RunInBackground(params Stream[] @params)
            {
                //Write your logic here
            }
            protected override void OnPostExecute(string result)
            {
               mDialog.Dismiss();
               //Show your result here
                }
            }`
    

    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

  • luieyluiey Member ✭✭

    @Ahsan_Siddique said:
    Hi,
    You need to work in this way like,

    `private ProgressDialog mDialog = new ProgressDialog(Application.Context);
    protected override void OnPreExecute()
            {
                mDialog.Window.SetType(Android.Views.WindowManagerTypes.SystemAlert);
                mDialog.Show();
            }
    
            protected override void OnProgressUpdate(params string[] values)
            {
                mDialog.SetMessage(values[0]);
            }
            protected override string RunInBackground(params Stream[] @params)
            {
                //Write your logic here
            }
            protected override void OnPostExecute(string result)
            {
               mDialog.Dismiss();
               //Show your result here
                }
            }`
    

    Override these method OnPostExecute , OnPreExecute and OnProgressUpdate.

    How to override? Do I need to create new activity? Because error appear when I insert the override method to MainActivity.cs class

    Mainactivity.OnPreExecute not suitable method found to override

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    Check it.

  • luieyluiey Member ✭✭

    @Ahsan_Siddique said:
    Check it.

    Ok, I see the different, mine use the public class MainActivity : Activity and yours using public 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

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    Bro you need to create a new Task for your logic.

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    Appcompat is just a design library that's used for layout designing not used for backend code.

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    Create a Task on your button click operation and in this task override these method write your logic for OnPreExecute , PostExecute etc.

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    If you want to use appcompat for your layout then you to install AppCompat package from Nuget package manager.

Sign In or Register to comment.