• 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");
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    Nginx降权启动
    Tomcat降权启动
    【转载】XSS学习笔记
    仪仗队(容斥,欧拉,打表)
    2012蓝桥杯
    HPU周赛题目解析
    蓝桥杯真题集2011
    cf公式专场-续
    24点游戏&&速算24点(dfs)
    Parallelogram Counting(平行四边形个数,思维转化)
  • 原文地址:https://www.cnblogs.com/tommyheng/p/6404150.html
Copyright © 2020-2023  润新知