Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ProgressDialog pop up too late, trouble with threads and asyncs

$
0
0

I've Already wasted a day and half trying to work arround this problem. :(

Im trying to open a progressDialog when I press a button from a control(calendar) I've made.
When the user press the button the code sets some variables and then based on those, gets data from a ViewModel to fill my Control.

The problem comes when I try to create a ProgressDialog before running all the code.
I need to update my control visually and for this I try to run the code on the main thread.
But if i run the code on the main thread the ProgressDialog will only pop up after 1,5 second along with the update I just made to the interface and vanish right away.

        async Task right_Clicked(object sender, EventArgs e)
        {
            if (OnCalendarChangeBegin != null)
                OnCalendarChangeBegin(this); // THIS IS THE EVENT HANDLER THAT OPENS THE PROGRESSDIALOG
            ...
                await RELOAD(7);
            ...
        }

public async Task Reload(daysDifference)
{
            // DIALOG WAS ALREADY OPEN IN HERE BUT ITS NOT SHOWN
            this.StartDate = this.StartDate.AddDays(daysDifference);
            var aux = days.Children;
            ObservableCollection<DateTime> dataSource = new ObservableCollection<DateTime>();
            DayLayout day;

            Task myTask = Task.Factory.StartNew( () =>
            {
                dataSource =  new ObservableCollection<DateTime>(App.Instance.FreeTimeSlotViewModel.DaysWithFreeSlots.Select(x => x.Date));

            }).ContinueWith(_ =>
            {
                // THIS WORKS BUT THE DIALOG POPS UP TOO LATE
                Device.BeginInvokeOnMainThread(() =>
                {
                    for (int i = 0; i < aux.Count; i++)
                    {
                        day = ((DayLayout)aux[i]);
                        day.SetDate(daysDifference);
                        day.HasEvent = dataSource.Contains(day.DayDate.Date);
                    }


                    UpdateDaysUI(daysDifference, dataSource);                       // WILL UPDATE EACH DAY UI
                    OnCalendarChangeEnd(this);                                                 // EVENT HANDLER THAT HIDES THE PROGRESSDIALOG
                    SelectDay(StartDate);
                });
                // 

            });


            await myTask;
}

When I try to remove it from the main thread it will open the PROGRESS DIALOG but it only updates the first day text,
All the other days will keep their previous value, and once i try to use the application it starts having black screens, labels, buttons vanish and it becomes unsuable. Its almost like I've crashed the thread but the app keeps running.

on my android
i just use this to show the ProgressDialog

[assembly: Xamarin.Forms.Dependency(typeof(ShowHud_Droid))]
namespace ...
{
    public class ShowHud_Droid : IHud
    {
        public static ProgressDialog mDialog = new ProgressDialog(MainActivity.ctx);

        public ShowHud_Droid()
        {
            mDialog = new ProgressDialog(MainActivity.ctx);
            mDialog.Indeterminate = true;     
        }

        public void ShowHud(string text)
        {
            try
            {
                mDialog.SetMessage(text);
                mDialog.SetCancelable(true); 
                mDialog.Show();

                return true; 
            }
            catch (Exception e)
            {
                Console.WriteLine("Show HUD error: " + e.Message);
                throw e;
            }
        }

Any idea what I should attempt next?
Let me know if you need more details.

thank you for your support


Viewing all articles
Browse latest Browse all 58056

Trending Articles