C# 5.0 使用任务调试表TaskScheduler来运行task
来源:互联网 发布:java中如何处理高并发 编辑:程序博客网 时间:2024/05/29 05:08
示例效果与winform中this.invoke(new delegete{})跨线程操作资源类似
建立WPF项目,使用4.5框架
mainwindow.xml文件如下
<Window x:Class="TaskSchedulerDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TaskSchedulerDemo" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBlock Name="ContentTextBlock" HorizontalAlignment="Left" Margin="44,134,0,0" VerticalAlignment="Top" Width="425" Height="40" /> <Button Content="Sync" HorizontalAlignment="Left" Margin="45,190,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" /> <Button Content="Async" HorizontalAlignment="Left" Margin="165,190, 0, 0" VerticalAlignment="Top" Width="75" Click="Button_Click_1" /> <Button Content="Async OK" HorizontalAlignment="Left" Margin="285,190,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2" /> </Grid></Window>
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{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> 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<string> 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<string> task = TaskMethod(TaskScheduler.FromCurrentSynchronizationContext()); task.ContinueWith(t => { Mouse.OverrideCursor = null; }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); } Task<string> TaskMethod() { return TaskMethod(TaskScheduler.Default); } Task<string> 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); } }}
运行如下:
注意异常提示的区别,中文为程序运行产生,英文为计划输出
0 0
- C# 5.0 使用任务调试表TaskScheduler来运行task
- C# 任务Task的使用
- spring TaskScheduler 来实现定时任务
- C#基础--使用Task创建任务
- C# 异步线程任务使用Task await
- TaskScheduler内幕天机:Spark shell案例,TaskScheduler和SchedulerBackend、FIFO与FAIR、Task运行时本地性算法详解
- C#学习之Task.ContinueWith(连续的任务)的使用
- Spark 源码解析:彻底理解TaskScheduler的任务提交和task最佳位置算法
- live555 计划任务(TaskScheduler)
- C#多线程编程Task任务
- 36 Spark之TaskScheduler Spark shell案例运行日志详解、TaskScheduler和SchedulerBackend、FIFO与FAIR、Task运行时本地性算法详
- 第36课: TaskScheduler内幕天机解密:Spark shell案例运行日志详解、TaskScheduler和SchedulerBackend、FIFO与FAIR、Task运行时本地性算法
- C#查看Windows系统Task Scheduler内任务的运行状态
- 任务表 (Task Trees)
- C#异步编程之:(三)使用TaskScheduler.UnobservedTaskException
- (C#)使用Task创建任务
- task任务使用、js定时!!!
- C# TASK 使用实例
- 近200篇机器学习&深度学习资料分享
- hdu 1518 square
- Uva The Tower of Babylon
- Started Service 与 Bound Service
- Cpp_类和对象
- C# 5.0 使用任务调试表TaskScheduler来运行task
- UVA 540(p117)----Team Queue
- 在CentOS上MySQL数据库服务器配置方法
- BZOJ-2748 音量调节 DP+背包(脑残)
- APP引导页Demo
- UVA 548(p155)----Tree
- 嵌入式的一些面试问题
- JS下实现加号减号的数量加一减一
- MD5 delPhi 加密