Typically, when you are trying to do asynchronous tasks in a view-model, the code looks like so (simplified):
public class MyViewModel
{
private CancellationTokenSource CTS { get; set; }
public async Task Process()
{
CTS = new CancellationTokenSource();
try
{
await LongRunningTask(CTS.Token);
} catch (OperationCanceledException) { }
}
public async Task Cancel()
{
CTS.Cancel();
}
}
The problem is that CancellationTokenSource
is an IDisposable
. Does that mean we simply place it in a using
block, or is there something more to it since it is stored in a private property?