I want to cancel a thread and and run another one just after. Here is my code:
private void ResetMedia(object sender, RoutedEventArgs e)
{
cancelWaveForm.Cancel(); // cancel the running thread
cancelWaveForm.Token.WaitHandle.WaitOne(); // wait the end of the cancellation
cancelWaveForm.Dispose();
//some work
cancelWaveForm = new CancellationTokenSource(); // creating a new cancellation token
new Thread(() => WaveFormLoop(cancelWaveForm.Token)).Start(); // starting a new thread
}
When I call this method, the first thread doesn't stop and the second one start running...
But if I skip the two last line it works :
private void ResetMedia(object sender, RoutedEventArgs e)
{
cancelWaveForm.Cancel(); // cancel the running thread
cancelWaveForm.Token.WaitHandle.WaitOne(); // wait the end of the cancellation
cancelWaveForm.Dispose();
//some work
//cancelWaveForm = new CancellationTokenSource(); // creating a new cancellation token
//new Thread(() => WaveFormLoop(cancelWaveForm.Token)).Start(); // starting a new thread
}
Why it doesn't stop?
Edit 1 :
private void WaveFormLoop(CancellationToken cancelToken)
{
try
{
cancelToken.ThrowIfCancellationRequested();
//some stuff to draw a waveform
}
catch (OperationCanceledException)
{
//Draw intitial Waveform
ResetWaveForm();
}
}