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

      

  • 相关阅读:
    Dos命令%date:~0,10%
    SharePoint显示错误信息
    VM打开虚拟机文件报错
    Sharepoint2013切换用户菜单
    批量创建域用户
    通过数据库恢复SharePoint网站
    SharePoint2013修复报错
    js 循环
    js 正则表达式
    阶乘函数
  • 原文地址:https://www.cnblogs.com/tommyheng/p/6404150.html
Copyright © 2011-2022 走看看