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

      

  • 相关阅读:
    查找之折半查找
    排序之快速排序
    排序之插入排序
    配置nginx支持path_info模式
    安装hadoop2.7.3
    Intellij Idea配置MapReduce编程环境
    jenkins+webhook+docker做持续集成
    nginx反向代理
    docker commit使用
    jenkins容器权限被拒绝
  • 原文地址:https://www.cnblogs.com/tommyheng/p/6404150.html
Copyright © 2011-2022 走看看