简体   繁体   English

取消令牌和线程不起作用

[英]Cancellation token and thread not working

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 : 编辑1:

private void WaveFormLoop(CancellationToken cancelToken)
{
        try
        {
            cancelToken.ThrowIfCancellationRequested();
            //some stuff to draw a waveform
        }
        catch (OperationCanceledException) 
        {
            //Draw intitial Waveform
            ResetWaveForm();
        }
}

Using CancellationTokens is called "Co-operative cancelation" because the code must co-operate with the cancelation action. 使用CancellationTokens称为“合作取消”,因为代码必须与取消动作合作。 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();
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM