JAVAJoseph
JAVAJoseph

Reputation: 31

Xamarin Loading Message while Awating Methods

I'm currently attempting to generate a Loading message which will run upon the completion of two method calls. The methods are SetData and FindNotesPerDay.

I have been using a Progress Dialog when attempting this however I am having no luck, either the progress dialog doesn't open or it appears but doesn't load the methods or disappear.

Below is the code is the code without my attempt to try a loading dialog.

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Create your application here
    SetContentView(Resource.Layout.TimeTable);

    expandableListView = FindViewById<ExpandableListView>(Resource.Id.ListViewExpanded);
    StartMonday = FindViewById<TextView>(Resource.Id.TextViewStartDateMonday);
    SelectDateButton = FindViewById<Button>(Resource.Id.SelectDateButton);
    NotesListView = FindViewById<ListView>(Resource.Id.NotesListView);

    // Populate User 
    User = JsonConvert.DeserializeObject<UserInstance>(Intent.GetStringExtra("User"));

    //Find Monday 
    DateTime TodaysDate = DateTime.Now;
    while (TodaysDate.DayOfWeek != DayOfWeek.Monday) TodaysDate = TodaysDate.AddDays(-1);

    StartMonday.Text = "Starting On " + TodaysDate.ToLongDateString();

    // Loading Message While These Load!!!!!!!!!!!!!!!!!!
    SetData(TodaysDate, out myAdapater);
    FindNotesForDay(TodaysDate, TodaysDate.AddDays(+6));

    SelectDateButton.Click += delegate {
        DateSelectOnClick();
    };
}

below is my attempt to display a loading message

ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);

progress.SetMessage("Contacting server. Please wait...");
progress.SetCancelable(true);
progress.Show();


var progressDialog = ProgressDialog.Show(this, "Please wait...", "Checking account info...", true);

//Find Monday 
DateTime TodaysDate = DateTime.Now;
while (TodaysDate.DayOfWeek != DayOfWeek.Monday) TodaysDate = TodaysDate.AddDays(-1);

new Thread(new ThreadStart(delegate
{
    RunOnUiThread(() => Toast.MakeText(this, "Toast within progress dialog.", ToastLength.Short).Show());
    RunOnUiThread(() => SetData(TodaysDate, out myAdapater));
    RunOnUiThread(() => FindNotesForDay(TodaysDate, TodaysDate.AddDays(+6)));
    RunOnUiThread(() => progressDialog.Hide());

})).Start()

Upvotes: 0

Views: 1460

Answers (2)

JAVAJoseph
JAVAJoseph

Reputation: 31

This was my solution however i must say the spinner currently isn't working though the dialog does

Thanks, Joe

namespace PleaseWorkV1
{
[Activity(Label = "TimetableMenu")]
public class TimetableMenu : Activity
{
    public Button ClassTimetableButton;
    public Button PlacementTimetableButton;
    public Button DownloadCalBTN;
    public UserInstance User;
    public GetConnectionClass Connect = new GetConnectionClass();

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.TimeTableMenu);

        ClassTimetableButton = FindViewById<Button>(Resource.Id.ClassTimeTableBTN);
        PlacementTimetableButton = FindViewById<Button>(Resource.Id.PlacementTimeTableBTN);
        DownloadCalBTN = FindViewById<Button>(Resource.Id.DownloadMonthBTN);



        User = JsonConvert.DeserializeObject<UserInstance>(Intent.GetStringExtra("User"));

        ClassTimetableButton.Click += async (sender, args) => await ClassOnClick();

        //ClassTimetableButton.Click += delegate
        //{
        //    ClassOnClick();
        //};

        PlacementTimetableButton.Click += delegate
        {
            PlacementOnClick();
        };

        DownloadCalBTN.Click += delegate
        {
            DownloadCalBTNOnClick();
        };
    }


    private async Task ClassOnClick()
    {
        var progressDialog = ProgressDialog.Show(this, "Loading", "Message", false);

        var MoveToTimeTable = new Intent(this, typeof(TimeTable));
        MoveToTimeTable.PutExtra("User", JsonConvert.SerializeObject(User));
        StartActivity(MoveToTimeTable);

        await Task.Delay(0)
            .ContinueWith(task => { StartActivity(MoveToTimeTable); });

        progressDialog.Hide();

    }

Upvotes: 0

Grace Feng
Grace Feng

Reputation: 16652

I have been using a Progress Dialog when attempting this however I am having no luck, either the progress dialog doesn't open or it appears but doesn't load the methods or disappear.

You can create a AsyncTask for this work, for example:

private class MyTask : AsyncTask
{
    private ProgressDialog progress;
    private Context context;

    public MyTask(Context mcontext)
    {
        context = mcontext;
    }

    protected override void OnPreExecute()
    {
        base.OnPreExecute();
        progress = new ProgressDialog(context);
        progress.Indeterminate = true;
        progress.SetProgressStyle(ProgressDialogStyle.Spinner);

        progress.SetMessage("Contacting server. Please wait...");
        progress.SetCancelable(true);
        progress.Show();
    }

    protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
    {
        //do your work here and return the result
        PublishProgress(10);
        return null;
    }

    protected override void OnProgressUpdate(params Java.Lang.Object[] values)
    {
        base.OnProgressUpdate(values);
        Task.Delay(2000).ContinueWith(t =>
        {
            progress.SetMessage("Checking account info...");
        }, TaskScheduler.FromCurrentSynchronizationContext());
    }

    protected override void OnPostExecute(Java.Lang.Object result)
    {
        base.OnPostExecute(result);
        Task.Delay(5000).ContinueWith(t =>
        {
            progress.Dismiss();
        }, TaskScheduler.FromCurrentSynchronizationContext());
    }
}

You can call this task like this:

var task = new MyTask(this);
task.Execute();

This is just a sample, you should be able to replace the task delay part to your logical code.

Upvotes: 2

Related Questions