Arun manthiram
Arun manthiram

Reputation: 145

progressbar and thread in xamarin android

I am using thread and progressbar in xamarin android. I am using the following code for progress. But the progressbar is still showing after the thread has completed. Could anyone help me to resolve this:

ProgressBar probar=view.FindViewById<ProgressBar>(Resource.Id.progressBar1);
probar.Visibility = ViewStates.Invisible;
Button btnref = view.FindViewById<Button>(Resource.Id.btnrefresh);
    btnref.Click += delegate {
        probar.Visibility = ViewStates.Visible;
        System.Threading.ThreadStart th = new System.Threading.ThreadStart(imagesetting);
        Thread myThread = new Thread(th);
        myThread.Start();
        probar.Visibility = ViewStates.Invisible;
    };



   private void imagesetting() {
        string uri = "example.com/xxx.svc" + Vid + "";
    -----
    }

Thanks in Advance, Manthiram C

Upvotes: 0

Views: 956

Answers (1)

Taier
Taier

Reputation: 2119

1) Make method that hides/show probar on main thread

private void ShowProgressBar(bool show) {
    RunOnUiThread(() => {
      ProgressBar probar=view.FindViewById<ProgressBar>(Resource.Id.progressBar1);
      probar.Visibility = show ? ViewStates.Visible : ViewStates.Invisible; 
    });
}

2) Show probar before starting task thread by calling ShowProgressBar(true)

btnref.Click += delegate {
    ShowProgressBar(true);
    System.Threading.ThreadStart th = new System.Threading.ThreadStart(imagesetting);
    Thread myThread = new Thread(th);
    myThread.Start();
 };

3) Hide probar by calling ShowProgressBar(false) at the end of imagesetting

 private void imagesetting() {
    string uri = "example.com/xxx.svc" + Vid + "";

    *some code*

    ShowProgressBar(false);
 }

Upvotes: 4

Related Questions