Asynchronous method

Asked

Viewed 36 times

1

Good morning, I have an ASP.NET MVC 5 application that opens IT calls, I have a procedure that triggers e-mail to users at the end of the task execution (completion, opening, forwarding, etc. of the calls). I need to turn this email firing procedure into asynchronous, and I did that. But I couldn’t make the procedure call in Action since Action is not asynchronous.

That way, I know that the activity of sending email continues non-synchronistic. To define it asynchronous I have to define the entire Action also asynchronous? I can’t just define the procedure?

[HttpPost]
public ActionResult DetalhaChamado(Chamado objChamado)
{
    // .. . AÇÕES DA ACTION
    if (email != null)
        Utilidades.Utilidades.EnviaEmailAsync(email.destinatario, Mensagem, email.nome, id_chamado); // DESSA FORMA, A ACTION CONTINUA A EXECUTAR A TAREFA DE MANEIRA NÃO ASSÍNCRONA        
}

1 answer

0


Create an async method within your method and try to make the call, remembering that your email sending method should be async:

    public ActionResult DetalhaChamadao(Chamado objChamado)
    {
        // .. . AÇÕES DA ACTION
        if (email != null)
            Task task = new Task(() => Enviar(item.destinatario, Mensagem, item.nome, id_chamado));
            task.Wait(1000);
            task.Start();
        async void Enviar(string destinatario, string mensagem, string nome, int chamadoId)
        {
            await Utilidades.Utilidades.EnviaEmail(destinatario, mensagem, nome, chamadoId);
        }
    }

Browser other questions tagged

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