How to create an asynchronous method that is cancelable?
In this context, the DoFoo()
method does things that can not be simply stopped, such as reading and writing files, and by canceling, I have to wait for these I / O operations to complete for the method to be canceled. / p>
private async void Button_Click()
{
await DoFoo();
}
private async void Cancelar_Click()
{
// cancelamento do método DoFoo()
// ...
}
private async Task DoFoo()
{
// operações de I/O
File.WriteAllText("path", "conteudo");
// operações de longa execução
// ...
}