Jayson E Garcia
Jayson E Garcia

Reputation: 59

Progress bar and thread in Xamarin Android C#

I try to load my SQL using this method. When I clicked the button login, the method is working and it shows the circle progress dialog but after the progress dialog complete or the thread was complete the circle progress dialog still appear.

I want the circleprogressbar to disappear using RunOnUiThread(). How can I to figure what is wrong with my code? Incidentally, I ran this in my fragment so I need to use Activity to call the RunOnUiThread function.

circleprogressbar.Visibility = ViewStates.Visible;
new Thread(new ThreadStart(delegate
    {

       while (progressValue < 100)
           {
               progressValue += 10;
               circleprogressbar.Progress = progressValue;
                Thread.Sleep(2000);
           }
  Activity.RunOnUiThread(() => { circleprogressbar.Visibility = ViewStates.Invisible; });
            })).Start();

Upvotes: 0

Views: 696

Answers (1)

Smitter
Smitter

Reputation: 86

I would suggest to create event to handle this situation, like this:

public static class SomeClassWithEventDeclaration
{
    public event EventHandler<SomeClassType> Completed;
}

public class YourClassWithWork
{
    public void Work()
    {
        SomeClassWithEventDeclaration.Completed += (sender, data) =>
        { 
           CheckTheData(data);
           HideProgressBar();
        };
    }

    public void MethodWithDataProcessing()
    {
        try
        {
           //Your processing
           SomeClassWithEventDeclaration.Completed.Invoke(someData);
        }
        catch(Exception ex)
        {
           //SomeClassWithEventDeclaration.Completed.Invoke(someErrorData);
        }
    }
}

I wrote that code without intellisense, so some declarations or method callings might require some check. This solution is not great, but if you need it right now - that would help. Also, I would suggest you to read about Xamarin Forms - Messaging Center. This tool would be better to use in your situation.

Upvotes: 1

Related Questions