Voici mon problème: J'annule une tâche avec un événement Click qui fonctionne correctement. Maintenant, je veux redémarrer la tâche en cliquant sur le même événement de démarrage qui a commencé la tâche initialement. L '"erreur" que j'obtiens est que j'obtiens les informations de MessageBox ("Arrêté Cliqué"). Donc je suis "coincé" dans la tâche de nettoyage.WPF TPL Redémarrer une tâche annulée
Comment résoudre ce problème? L'aide est très appréciée.
Merci!
Voici mon code:
public partial class MainWindow
{ CancellationTokenSource cts = new CancellationTokenSource();
ParallelOptions po = new ParallelOptions();
}
private void Start_Click(object sender, RoutedEventArgs e)
{ var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
CancellationToken token = cts.Token;
ParallelOptions po = new ParallelOptions();
po.CancellationToken = cts.Token;
po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
Task dlTask = Task.Factory.StartNew(() =>
{ do
{ token.ThrowIfCancellationRequested();
Parallel.For(0, dicQueryNoQueryURL.Count, po
, i =>
{ token.ThrowIfCancellationRequested();
if (!token.IsCancellationRequested){// do work
}
});
}
while (!token.IsCancellationRequested);
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
dlTask.ContinueWith(
(antecedents) =>
{ if (token.IsCancellationRequested){
MessageBox.Show("Stop Clicked");
}
else
{ MessageBox.Show("Signalling production end"); }
dlTask.Dispose();
}, uiScheduler);
}
private void btnStop_Click(object sender, RoutedEventArgs e){ cts.Cancel(); }
Cela a fait l'affaire. Merci beaucoup! merci aussi pour l'extrait! J'ai encore beaucoup à apprendre :( – user774326