zoukankan      html  css  js  c++  java
  • TASK的开始与暂停

    namespace WpfApplication1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            // create the cancellation token source  
            private CancellationTokenSource TokenSource = new CancellationTokenSource();
            private Task CurrentTask;
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void btnStart_Click(object sender, RoutedEventArgs e)
            {
                // create the cancellation token  
                CancellationToken token = TokenSource.Token;
                // create the task  
    
                CurrentTask = new Task(() =>
                {
                    for (int i = 0; i < int.MaxValue; i++)
                    {
                        if (token.IsCancellationRequested)
                        {
                            Debug.WriteLine("Task cancel detected");
                            break;
                            //throw new OperationCanceledException(token);
                        }
                        else
                        {
                            Debug.WriteLine("Int value {0}", i);
                        }
                    }
                }, token);
    
    
                // start the task  
                CurrentTask.Start();
            }
    
            private void btnStop_Click(object sender, RoutedEventArgs e)
            {
                if (CurrentTask != null)
                {
                    if (CurrentTask.Status == TaskStatus.Running) { }
                    {
                        TokenSource.Cancel();
                        Debug.WriteLine("Task cancel");
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    自己收集的一些伪元素/伪类
    ie9的placeholder不显示的解决办法(包含多个密码框)
    9.14上午
    9.13
    9.11笔记
    html基础英语单词
    选择器的分辨
    学习笔记
    RecyleView
    自定义view获取宽高
  • 原文地址:https://www.cnblogs.com/tommyheng/p/6404150.html
Copyright © 2011-2022 走看看