Reputation: 607
I have a progress bar in my activity.
When I click button I want to do something. I try to simulate this process with this code ( code when a button pressed):
void btn_clicked(object sender, EventArgs e){
int progressStatus = 0;
while (progressStatus < 100)
{
progressStatus += 1;
progressBar.SetProgress(progressStatus, false);
Thread.Sleep(1000);
}
}
I try to do that code, but it did not update the progress.
But when I do something like this:
void btn_clicked(object sender, EventArgs e){
progressBar.SetProgress(progressBar.progress += 1, false);
}
It works.
I have to try run set progress in RunOnUiThread, RunOnUiThread, or Handler.Post but when I have to loop in my method. It makes progress bar did not update.
In While loop, I have try this:
while (progressStatus < 100)
{
progressStatus += 1;
// I also have try to change "RunOnUIThreadSynchronous"
// with Handler.Post" or "RunOnUIThread"
this.RunOnUIThreadSynchronous(() =>{
progressBar.SetProgress(progressStatus, false);
});
Thread.Sleep(1000);
}
Can someone help me?
Upvotes: 0
Views: 1070
Reputation: 26
I have to try run set progress in RunOnUiThread, RunOnUiThread, or Handler.Post but when I have to loop in my method. It makes progress bar did not update. Can someone help me?
It sounds like you are trying to carry out your looping method in the UI thread which would explain the bar not changing value during the loop. Try running your method in a new/different thread.
Upvotes: 1
Reputation: 27
I believe the issue is due to code line below:
int progressStatus = 0;
In the first function, above line of code resets progressStatus to zero whereas in the second function, it does not initialize the variable. Try moving the initialization to outside the scope of function probably a line before the function declaration. This should work:
int progressStatus = 0;
void btn_clicked(object sender, EventArgs e){
while (progressStatus < 100)
{
progressStatus += 1;
progressBar.SetProgress(progressStatus, false);
}
}
Upvotes: 0