简体   繁体   English

取消异步任务?

[英]Cancel async task?

My code isn't canceling the tasks correctly and I'm still seeing my series in my graph being drawn for the next series in the foreach loop... not sure what I'm doing wrong here as I want to exit and cancel all async tasks at this point... any ideas? 我的代码未正确取消任务,我仍然看到我的图中的系列是为foreach循环中的下一个系列绘制的……不确定我在做什么错,因为我要退出并取消所有此时的异步任务...有什么想法吗?

    private void StartTest_Click(object sender, RoutedEventArgs e)
    {
        var cancellationTokenSource = new CancellationTokenSource();
        if (_isRunning)
        {
            cancellationTokenSource.Cancel();
        }

        _isRunning = !_isRunning;
        Start(cancellationTokenSource.Token);
    }

    private async void Start(CancellationToken cancellationToken)
    {
        foreach (var buttonSelected in selectedButtons)
        {
            // If cancellation requested
            if (cancellationToken.IsCancellationRequested)
                break;

            // Retrieve series to reflect changes on
            var seriesToChange = Model.Series.Where(x => x.Title == buttonSelected.Name).ToArray();

            // Create timer
            var timerForPlotting = new DispatcherTimer();
            if (seriesToChange .Length == 1)
            {
                // Set the series to visible
                seriesToChange [0].IsVisible = true;

                timerForPlotting.Interval = TimeSpan.FromMilliseconds(50);
                timerForPlotting.Tick += (object s, EventArgs a) => PlotSeriesPoints_Tick(s, a, seriesToChange [0]);
            }

            // Start
            InitiateTimerWithButtonUIChange(timerForPlotting, buttonSelected, false);

            // Set the task to only take a couple of seconds
            await Task.Delay(2000);

            // End
            InitiateTimerWithButtonUIChange(timerForPlotting, buttonSelected, true);
        }
    }

    private void InitiateTimerWithButtonUIChange(DispatcherTimer timer, Button buttonSelected, bool isFinished)
    {
        if (!isFinished)
        {
            timer.Start();
            buttonSelected.Background = resourceDictionary["Processing"] as Brush;
        }
        else
        {
            timer.Stop();
            buttonSelected.Background = resourceDictionary["ColourActive"] as Brush;

            // Reset
            time = 0;
        }
    }

Try to call Cancel() on the actual CancellationTokenSource that you used to create the token that you passed to Start : 尝试在用于创建传递给Start的令牌的实际CancellationTokenSource上调用Cancel()

CancellationTokenSource cancellationTokenSource;
private void StartTest_Click(object sender, RoutedEventArgs e)
{
    if (cancellationTokenSource != null)
    {
        cancellationTokenSource.Cancel();
        cancellationTokenSource.Dispose();
    }

    cancellationTokenSource = new CancellationTokenSource();
    _isRunning = !_isRunning;
    Start(cancellationTokenSource.Token);
}

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

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