In Xamarin/C#, How do I display the progress bar from the middle of a task instead of at completion? RunOnUiThread isn't doing the job.
I'm writing an android app using Xamarin & C#. My first activity is a login window. I don't want to move on to the next activity until login is successful. I want to display a progress bar while I check the credentials. This is where I'm stuck.
Though this is an async task, I'm doing Task.Wait() to keep from moving to the next activity before login is validated. Because of Task.Wait(), my progressBar.Visibility = ViewStates.Visible also doesn't display until the task.Wait() is finished, which, obviously is too late.
RunOnUiThread isn't doing it. I've found answers for Java, using OnPreExecute but I can't figure out how to implement that in C#.
So again, given the event of clicking a button, how do you display the progress bar before doing the rest of the task?
UPDATE:
After implementing the solution from @Leon Lu - MSFT I still don't see the progressbar become visible. And apparently the next intent is started before logintask is complete. I can see it executing the steps in debug, but still the progressbar doesn't appear.
The progressbar does appear if I set it visible in OnCreate, so I know it's there. Perhaps I implemented the solution wrong.
Login button click handler:
[Export("btnLogin_Clicked")]
public async void btnLogin_Clicked(View view)
{
LoginActivityAsync logintask = new LoginActivityAsync(progressBar, editUsername.Text, editPassword.Text, AuthSvr);
logintask.Execute();
{
Intent MainIntent = new Intent(this, typeof(MainActivity));
MainIntent.PutExtra("ticket", _ticket);
MainIntent.PutExtra("poapiurl", ApiSvr);
StartActivityForResult(MainIntent, 0);
}
}
LoginActivtyAsync:
public class LoginActivityAsync : AsyncTask<string, int, string>
{
ProgressBar _progressbar;
private string _user;
private string _password;
private string _authsvr;
public LoginActivityAsync(ProgressBar progressBar, string user, string password, string authSvr)
{
_progressbar = progressBar;
_user = user;
_password = password;
_authsvr = authSvr;
}
protected override void OnPreExecute()
{
_progressbar.Visibility = ViewStates.Visible;
base.OnPreExecute();
}
protected override void OnPostExecute(Java.Lang.Object result)
{
base.OnPostExecute(result);
_progressbar.Visibility = ViewStates.Invisible;
}
protected override string RunInBackground(params string[] @params)
{
string ticket;
try
{
ticket = newGetAuthTicket(_authsvr, _user, _password);
}
catch (System.Exception ex)
{
throw new System.Exception(ex.Message);
}
return ticket;
}
