I want if the user changed the listbox item and the previous task was not complete yet, that task stops and a new task starts. How can i do this?
I used this code but not work.
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token;
private void Listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (task != null && (task.IsCompleted == false || task.Status == TaskStatus.Running))
{
if (source != null)
source.Cancel();
}
task = LoadArts();
}
private async Task LoadArts()
{
token = source.Token;
await Task.Run(() =>
{
}
, token);
}