3
How to create an asynchronous method that is cancellable?
In this context, the DoFoo()
does things that cannot simply be stopped, like reading and writing files, and when canceling, I have to wait for those I/O operations to be completed for the method to be canceled.
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
// ...
}
Depending on what you are running, you need to have the opportunity to perform the cancellation.
– Maniero