title: Async Task to return a boolean using a method
body: <p>I have an Async task that I am using to call a method that then will return a boolean. </p>
  
  <pre><code>   public async Task LoginAsync(){
  
              await Task.Run(() =&gt;
              {
                  CheckUsernameLogin();
              });
          }
  </code></pre>
  
  <p>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 <code>bool x = LoginAysnc();</code>. I have tried making the async method look something like this </p>
  
  <pre><code>   public async Task&lt;bool&gt; LoginAsync(){
  
              return await Task.Run(() =&gt;
              {
                  CheckUsernameLogin();
              });
          }
  </code></pre>
  
  <p>then tried to assign the return value with something like this <code>bool x = LoginAsync.Result;</code> 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. </p>
  
  <p>EDIT!</p>
  
  <p>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. </p>
  
  <pre><code> public async Task&lt;bool&gt; CheckUsernameLogin(){
          MySqlConnection connection = new MySqlConnection("Top Secret Server Stuff");
          connection.Open();
          username = FindViewById &lt;TextView&gt;(Resource.Id.username_field).Text;
          TriTrack.Utils.Settings.LastUsername = FindViewById&lt;TextView&gt;(Resource.Id.username_field).Text;
          password = FindViewById&lt;TextView&gt;(Resource.Id.password_field).Text;
          TriTrack.Utils.Settings.LastPassword = FindViewById&lt;TextView&gt;(Resource.Id.password_field).Text;
          string user_password;
          List&lt;string&gt; names = new List&lt;string&gt;();
          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;
      }
  </code></pre>