I have an Async task that I am using to call a method that then will return a boolean.
public async Task LoginAsync(){
await Task.Run(() =>
{
CheckUsernameLogin();
});
}
I am also updating the GUI elsewhere while this is happening. I want to use LoginAsync() to assign a value to a boolean i.e bool x = LoginAysnc();
. I have tried making the async method look something like this
public async Task<bool> LoginAsync(){
return await Task.Run(() =>
{
CheckUsernameLogin();
});
}
then tried to assign the return value with something like this bool x = LoginAsync.Result;
but that deadlocks my app. I am doing it this way to check if the username login credentials (determined by CheckUsernameLogin) are valid while also updating the GUI to hold a progress bar. I want to use the returned boolean value to create AlertDialogs based on the validity of the credentials. Im very frustrated and stuck. Thanks in advance.
EDIT!
Here is the other method. Some users suggested making CheckUsernameLogin async, but I don't know where I would but the await calls. I also know that my AlertDialogs will not work because I am using this method in an async way or something like that. Im VERY new to await/sync stuff. Thanks guys.
public async Task<bool> CheckUsernameLogin(){
MySqlConnection connection = new MySqlConnection("Top Secret Server Stuff");
connection.Open();
username = FindViewById <TextView>(Resource.Id.username_field).Text;
TriTrack.Utils.Settings.LastUsername = FindViewById<TextView>(Resource.Id.username_field).Text;
password = FindViewById<TextView>(Resource.Id.password_field).Text;
TriTrack.Utils.Settings.LastPassword = FindViewById<TextView>(Resource.Id.password_field).Text;
string user_password;
List<string> names = new List<string>();
var command = connection.CreateCommand();
command.CommandText = ("SELECT username FROM users where username=@username;");
command.Parameters.AddWithValue("@username", username);
MySqlDataReader username_reader = command.ExecuteReader();
while (username_reader.Read())
{
names.Add(username_reader.GetString("username"));
}
username_reader.Close();
if (names.Count != 1){
ShowAlert();
return false;
}
else{
command.CommandText = ("SELECT password FROM users where username=@username;");
command.Parameters.AddWithValue("@username", username);
MySqlDataReader password_reader = command.ExecuteReader();
password_reader.Read();
user_password = password_reader.GetString(0);
if(user_password == password){
Intent intent = new Intent(this, typeof(MapsActivity));
this.StartActivity(intent);
return true;
}
else{
ShowAlert();
}
}
return false;
}