Async method returning a List<string> how to use?

Asked

Viewed 403 times

2

I have a method called consulta_usuario_email, that is responsible for searching in the table of registered users, the emails of users of a certain department:

 public async Task<List<string>> consulta_usuario_email(string departamento)
    {
        consql.bd_string();
        SqlConnection sqlconn = new SqlConnection(consql.sqlconn);

        List<string> email = new List<string>();

        try
        {
            consql._sql = @"select a.Email
                            from AspNetUsers as a
                            left join Empresa_departamento as c
                            on a.Departamento = c.id
                            where c.departamento = @departamento";

            SqlCommand cmd = new SqlCommand(consql._sql, sqlconn);

            cmd.Parameters.Add("@departamento", SqlDbType.VarChar).Value = departamento;

            await sqlconn.OpenAsync();

            using (SqlDataReader leitor = await cmd.ExecuteReaderAsync())
            {
                while (leitor.Read())
                {
                    email.Add(leitor["Email"].ToString());
                }
            } 
        }
        catch (Exception error)
        {
            MessageBox.Show("Erro" + "\n" + error);
        }
        finally
        {
            sqlconn.Close();
        }

        return email;
    } // consulta o email do usuario conforme seu departamento

I want to execute that method Async, and then when this is completed, I want to execute another method that receives as parameters the List generated by the method consulta_usuario_email

 Task<List<string>> Mail_PCP = Email_User.consulta_usuario_email("PCP");

          if (Mail_PCP.IsCompleted == true)
          {
              Mail_Urgente.Send(Mail_Compras, Mail_PCP, "Teste", "Teste");
          }

However, how do I pass the List for the method Send class Mail_Urgente?

Update - ref. employee response @LINQ:

"Just wear await, like this:"

List<string> Mail_PCP = await Email_User.consulta_usuario_email("PCP");
Mail_Urgente.Send(Mail_Compras, Mail_PCP, "Teste", "Teste");

When trying to do as above the error is generated:

inserir a descrição da imagem aqui

1 answer

2


Just use await, thus.

List<string> Mail_PCP = await Email_User.consulta_usuario_email("PCP");
Mail_Urgente.Send(Mail_Compras, Mail_PCP, "Teste", "Teste");

Remember that the method that calls the consulta_usuario_email needs to be marked with async.

You can test this with this code:

using System;
using System.Threading.Tasks;
using System.Collections.Generic;

public class Program
{
    public async static void Main()
    {
        var lista = await TaskTeste();      
        lista.ForEach(Console.WriteLine);       
    }

    public async static Task<List<string>> TaskTeste()
    {
        return await Task.Run(() => (new List<string> { "LINQ", "teste" }));
    }
}

See working on . NET Fiddle.

  • friend, but thus is generated an error... I made a review on the question adding a picture of the error.

  • I don’t see any mistake.

  • I just updated the question

  • The error is self-explanatory. You have to mark the method called with async.

  • But the method consulta_usuario_email is already marked as Async as can be seen at the beginning of the question.

  • The calling method needs to be marked as async. It was my typo.

  • Perfect. I get it. Thank you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.