Dan Schneider
Dan Schneider

Reputation: 155

ProgressBar Visibility ignored

I am trying to display a progress bar when i start an async task, and after the async task is done running, set the progress bar back to invisible. For some reason the progressBar is not responding to the code that changes the visibility property.

        Activity.RunOnUiThread(() =>
        {
            prog.Visibility = ViewStates.Visible;
        });

        Task<bool> createPickTask = Task.Run(() => Utils.createPick(firstBet, myBet, game));
                bool createPickResult = createPickTask.Result;

        if (createPickResult)
        {
            adapter.NotifyItemChanged(pos);
        }
        else
        {
            showErrorMessage();
        }

        Activity.RunOnUiThread(() =>
        {
            prog.Visibility = ViewStates.Gone;
        });

Upvotes: 1

Views: 167

Answers (1)

Billy Liu
Billy Liu

Reputation: 2168

For some reason the progressBar is not responding to the code that changes the visibility property.

The code will not take effect on UI until this part of code is all executed. You could try to set the progress bar visibility in the Task. For example:

    private void Button_Click(object sender, System.EventArgs e)
    {
        Activity.RunOnUiThread(() =>
        {
            prog.Visibility = ViewStates.Visible;
        });

        Task<bool> createPickTask = Task.Run(/*async*/ () => {
            //await Task.Delay(3000);
            bool createPickResult = Utils.createPick(firstBet, myBet, game);

            if (createPickResult)
            {
                adapter.NotifyItemChanged(pos);
            }
            else
            {
                showErrorMessage();
            }

            Activity.RunOnUiThread(() =>
            {
                prog.Visibility = ViewStates.Gone;
            });
            return createPickResult;
        });
    }

And result is:

enter image description here

Upvotes: 2

Related Questions