Ignored await in asynchronous method c#

Asked

Viewed 145 times

3

I need to use the return of an asynchronous method for validation but even with the use of "await" the method is still running before I receive the return of the function.

    var teste = await new VendaService().EnviarVendaParaServicoCentral(new List<ItemVenda>(), new Venda());
    if (teste)
    {
       MessageBox.Show("feito");
    }

Asynchronous method with other asynchronous calls:

public async Task<bool> EnviarVendaParaServicoCentral(List<ItemVenda> itensVenda, Venda venda)
{
    try
    {
        await this._conHelper._connection.Start();
        this._vendaEfetuada = false;
        await this._conHelper._hubProxy.Invoke("RealizarVenda", itensVenda, venda);
        this._conHelper._hubProxy.On<bool>("RetornoDaVenda", (retornoServicoCentral) =>
            this.Dispatcher.BeginInvoke(() =>
            {
                this._vendaEfetuada = retornoServicoCentral;
            })
        );

        return this._vendaEfetuada;                
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return false;
}
  • @Jéfersonbueno that is the problem, I do not know what is wrong, he does not wait with the use of the await... the why is the doubt.

  • @Jéfersonbueno As far as I know, the await is used to force the wait.

  • 1

    Could you better explain the "method keeps running before I get the function return"? The variable value teste only exists after the function returns (actually, when its continuation is called, after the change that the compiler makes by await), then the continuation of the method (if (teste) ...) is only executed after its function returns. Is not the case? If the line if (teste)... is running, so the variable teste has a value, which was produced by the method that was awaited.

  • @That’s not the case.

1 answer

1


For asynchronous methods that return Task<object> in your case Task<bool>, you have to access the "Result" property Example: Method

public static async Task<int> test()
    {
        Task t = Task.Factory.StartNew(() => {   Console.WriteLine("do stuff"); });
        await t;
        return 10;
    }

Calling for

static  void Main(string[] args)
{

    Task<int> test1 = Task.Factory.StartNew<int>(() => test());
    System.Console.WriteLine(test1.Result); // block and wait for the result
    Console.ReadLine();
}

https://stackoverflow.com/questions/25599074/how-do-you-get-the-return-value-of-an-async-method

What you can also do is define a call back method for your method.

private async void Search()
{
    await Search(SearchCompleted);//<--pass the callback method here
}

private async Task Search(Action<string> callback)
{
    //Here you're done with the file so invoke the callback that's it
    callback(file);//pass which file is finished 
}

private void SearchCompleted(string file)
{
    //this method will be called whenever a file is processed
}

https://stackoverflow.com/questions/19003594/fire-callback-after-async-task-method

Browser other questions tagged

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