我想取消一个线程并在之后运行另一个线程。这是我的代码:
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
}
当我调用此方法时,第一个线程不会停止,第二个线程开始运行......
但如果我跳过最后两行,它就会起作用:
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
}
为什么它不停?
Edit 1 :
private void WaveFormLoop(CancellationToken cancelToken)
{
try
{
cancelToken.ThrowIfCancellationRequested();
//some stuff to draw a waveform
}
catch (OperationCanceledException)
{
//Draw intitial Waveform
ResetWaveForm();
}
}
使用 CancellationTokens 被称为“合作取消”,因为代码必须与取消操作合作。您只需在函数开始时检查一次,如果在该检查之后发生取消,则永远不会发生取消。
根据函数的名称,我假设其中存在某种循环。您的函数需要看起来像这样。
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();
}
}
现在它会检查循环的每次迭代是否发生取消。如果循环体需要很长时间来处理,您可能需要在循环内进行多个调用。
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)