如何正确重置的CancellationToken? [英] How do I reset a CancellationToken properly?

查看:352
本文介绍了如何正确重置的CancellationToken?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天早晨打轮异步CTP,并有一个按钮和一个标签一个简单的程序。单击该按钮并开始更新标签,阻止它停止写入labal按钮。但是,我不知道如何重置CancellationTokenSource这样我就可以重新启动该进程。我的code是低于

 公共部分类主窗口:窗口
    {
        CancellationTokenSource CTS =新CancellationTokenSource();
        公共主窗口()
        {
            的InitializeComponent();
            button.Content =开始;
        }        异步任务的DoWork(的CancellationToken cancelToken)
        {
            INT I = 0;
            而(!cancelToken.IsCancellationRequested)
            {
                label.Content =我++的ToString()。
                等待TaskEx.Delay(50 cancelToken);
            }
        }        私人无效Button_Click(对象发件人,RoutedEventArgs E)
        {
            如果(button.Content ==开始)
            {
                button.Content =停止;
                DoWork的(cts.Token);
            }
            其他
            {
                button.Content =开始;
                cts.Cancel();
            }
        }
    }


解决方案

您需要重新创建 CancellationTokenSource - 有没有办法复位你这一次设置

这可能是那样简单:

 私人无效Button_Click(对象发件人,RoutedEventArgs E)
{
    如果(button.Content ==开始)
    {
        button.Content =停止;
        cts.Dispose(); //清理旧令牌源....
        CTS =新CancellationTokenSource(); //重置取消标记源...
        DoWork的(cts.Token);
    }
    其他
    {
        button.Content =开始;
        cts.Cancel();
    }
}

I have been playing round with the async ctp this morning and have a simple program with a button and a label. Click the button and it starts updating the label, stop the button it stops writing to the labal.. however, i'm not sure how to reset the CancellationTokenSource so i can restart the process. My code is below

public partial class MainWindow : Window
    {
        CancellationTokenSource cts = new CancellationTokenSource();
        public MainWindow()
        {
            InitializeComponent();
            button.Content = "Start";
        }

        async Task DoWork(CancellationToken cancelToken)
        {
            int i = 0;
            while (!cancelToken.IsCancellationRequested)
            {
                label.Content = i++.ToString();
                await TaskEx.Delay(50, cancelToken);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (button.Content == "Start")
            {
                button.Content = "Stop";
                DoWork(cts.Token);
            }
            else
            {
                button.Content = "Start";
                cts.Cancel();
            }
        }
    }

解决方案

You need to recreate the CancellationTokenSource - there is no way to "reset" this once you set it.

This could be as simple as:

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (button.Content == "Start")
    {
        button.Content = "Stop";
        cts.Dispose(); // Clean up old token source....
        cts = new CancellationTokenSource(); // "Reset" the cancellation token source...
        DoWork(cts.Token);
    }
    else
    {
        button.Content = "Start";
        cts.Cancel();
    }
}

这篇关于如何正确重置的CancellationToken?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆