How to write an asynchronous method?

5

I have the following method:

public void Contar()
{
    int numero = 1;

    for (int i = 0; i < 100000; i++)
    {
        numero *= i
    }

    return numero
}

Assuming that the Contar() method takes so long to crash the UI of my application, how do I make the method asynchronous?

    
asked by anonymous 11.07.2017 / 04:09

1 answer

2

You can use the class Task

public async Task AguardarContadorAsync()
{
     int retorno = await Task.Run(()=>Contar());
}

As you may notice, you need to add the async Keyword in the method signature.

More information:

What are Async methods?

In C #, what is the keyword await?

    
11.07.2017 / 04:25