Using CancellationTokens is called "Co-operative cancelation" because the code must co-operate with the cancelation action. You only check once at the start of the function, if the cancelation happens after that check the cancelation never happens.
Based on the name of the function I assume there is a loop of some kind in it. Your function needs to look like this instead.
private void WaveFormLoop(CancellationToken cancelToken)
{
try
{
while(someCondition) //Replace this with your real loop structure, I had to guess
{
cancelToken.ThrowIfCancellationRequested();
//some stuff to draw a waveform
}
}
catch (OperationCanceledException)
{
//Draw intitial Waveform
ResetWaveForm();
}
}
Now it checks to see if cancelation happened every iteration of the loop. If the loop body takes a very long time to process you may want more than one call inside the loop.
private void WaveFormLoop(CancellationToken cancelToken)
{
try
{
while(someCondition) //Replace this with your real loop structure, I had to guess
{
cancelToken.ThrowIfCancellationRequested();
Thread.Sleep(1000); //Fake doing work
cancelToken.ThrowIfCancellationRequested();
Thread.Sleep(1000); //Fake doing more work
}
}
catch (OperationCanceledException)
{
//Draw intitial Waveform
ResetWaveForm();
}
}