C# 5.0 使用任务调试表TaskScheduler来运行task

示例效果与winform中this.invoke(new delegete{})跨线程操作资源类似

建立WPF项目,使用4.5框架

mainwindow.xml文件如下


    
        
        


mainwindow.xml.cs文件如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Threading.Tasks;

namespace TaskSchedulerDemo
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //同步运行任务,此时窗体将不作任何响应
            ContentTextBlock.Text = string.Empty;
            try
            {
                string result = TaskMethod().Result;
                ContentTextBlock.Text = result;
            }
            catch(Exception ex)
            {
                ContentTextBlock.Text = ex.InnerException.Message;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //异步方式运行任务,窗体可被移动,但抛出子线程访问主线程资源异常
            //但此种方式将存在异常的风险
            ContentTextBlock.Text = string.Empty;
            Mouse.OverrideCursor = Cursors.Wait;
            Task task = TaskMethod();
            task.ContinueWith(t =>
            {
                ContentTextBlock.Text = t.Exception.InnerException.Message;
                Mouse.OverrideCursor = null;
            }, 
            CancellationToken.None, 
            TaskContinuationOptions.OnlyOnFaulted,
            TaskScheduler.FromCurrentSynchronizationContext());
           
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //异步方式运行任务,窗体可被移动,子线程可访问主线程资源
            ContentTextBlock.Text = string.Empty;
            Mouse.OverrideCursor = Cursors.Wait;
            //区别在这
            Task task = TaskMethod(TaskScheduler.FromCurrentSynchronizationContext());
            task.ContinueWith(t =>
            {
                Mouse.OverrideCursor = null;
            },
            CancellationToken.None,
            TaskContinuationOptions.None,
            TaskScheduler.FromCurrentSynchronizationContext());
        }

        Task TaskMethod()
        {
            return TaskMethod(TaskScheduler.Default);
        }

        Task TaskMethod(TaskScheduler scheduler)
        {
            //采用task延迟方式可中断延迟,而thread.sleep()则不能
            //await Task.Delay(TimeSpan.FromSeconds(5), cancellationTokenSource.Token);
            Task delay = Task.Delay(TimeSpan.FromSeconds(5));

            return delay.ContinueWith(t =>
            {
                string str = string.Format("task is running on a thread id {0}. is thread pool thread: {1}",
                Thread.CurrentThread.ManagedThreadId,
                Thread.CurrentThread.IsThreadPoolThread);
                // ContentTextBlock为主线程资源
                ContentTextBlock.Text = str;
                return str;
            }, scheduler);
        }
    }
}


运行如下:

注意异常提示的区别,中文为程序运行产生,英文为计划输出

C# 5.0 使用任务调试表TaskScheduler来运行task_第1张图片

你可能感兴趣的:(c#,thread,task)