Task是什么
表示一个异步操作。它是异步操作的首选方式。Task还支持任务工厂的概念。任务工厂支持多个任务之间共享相同的状态,如取消类型CancellationTokenSource就是可以被共享的。通过使用任务工厂,可以同时取消一组任务。
Task以及Task.Factory都是在.Net 4引用的新特性,封装了以前的Thread,并管理Thread。
Task的优势
ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便。比如:
1: ThreadPool不支持线程的取消、完成、失败通知等交互性操作;
2: ThreadPool不支持线程执行的先后次序;
以往,如果开发者要实现上述功能,需要完成很多额外的工作,现在,FCL中提供了一个功能更强大的概念:Task。Task在线程池的基础上进行了优化,并提供了更多的API。在FCL4.0中,如果我们要编写多线程程序,Task显然已经优于传统的方式。
不管从工作函数是否有返回值,task都需要在其运行过程中至少有一个前台线程在跑,否则会直接退出,根本原因是所有task都是后台线程。task的工作函数的输入参数类型职能是object。其实Task跟线程池ThreadPool的功能类似,不过进行了更好的封装,写起来更为简单,直观。代码更简洁了,使用Task来进行操作。可以跟线程一样可以轻松的对执行的方法进行控制。
Task和Threadpool相比,Task能获取返回值、有更多的控制等,Threadpool占用的资源较小,如果只需要把任务丢到线程池中,其他的都不管的话,就建议使用Threadpool。配合CancellationTokenSource类更为可以轻松的对Task操作的代码进行中途终止运行。Run方法只接受无参的Action和Func委托,另外两个接受一个object类型的参数。可以调用Wait方法来阻塞当前线程,还可以通过Task.Result来获取返回值,当然它也会阻塞当前线程。接下来再说说常用的ContinueWith,这个说白了就是在某个任务执行完的延续,类似callback。continuewith接受action或func委托,委托的第一个参数都是task类型,可以通过它访问先前的task对象。
简单例子:
static void Main(string[] args)
{
Console.WriteLine("我是主线程:Thread Id {0}", Thread.CurrentThread.ManagedThreadId);
Task.Run(() => { DoDisplay1(); });
Task.Run(() => { DoDisplay2(); });
Task.Run(() => { DoDisplay3(); });
Task.Run(() => { DoDisplay4(); });
Task.Run(() => { DoDisplay5(); });
Task.Run(() => { DoDisplay6(); });
Task.Run(() =>
{
ReturnValueTest1();
});
Task<string> t1 = Task.Run<string>(() => { return ReturnValueTest2(); });
Console.WriteLine(t1.Result);
Console.ReadLine();
}
View Code
public static async void DoDisplay1()
{
await Task.Delay(1000);
Console.WriteLine(string.Format("DoDisplay1: current thread id----{0}", Thread.CurrentThread.ManagedThreadId));
}
public static async void DoDisplay2()
{
Console.WriteLine(string.Format("DoDisplay2 :current thread id----{0}", Thread.CurrentThread.ManagedThreadId));
}
public static async void DoDisplay3()
{
Console.WriteLine(string.Format("DoDisplay3 :current thread id----{0}", Thread.CurrentThread.ManagedThreadId));
}
public static async void DoDisplay4()
{
Console.WriteLine(string.Format("DoDisplay4 :current thread id----{0}", Thread.CurrentThread.ManagedThreadId));
}
public static async void DoDisplay5()
{
Console.WriteLine(string.Format("DoDisplay5 :current thread id----{0}", Thread.CurrentThread.ManagedThreadId));
}
public static async void DoDisplay6()
{
Console.WriteLine(string.Format("DoDisplay6 :current thread id----{0}", Thread.CurrentThread.ManagedThreadId));
}
public static async void ReturnValueTest1()
{
await Task.Delay(7000);
var s=Task.Run<string>(() => {
Console.WriteLine(string.Format("7s后 ReturnValueTest1 :current thread id----{0}", Thread.CurrentThread.ManagedThreadId));
return "ReturnValueTest1 hello";
});
Console.WriteLine(s.Result);
}
public static async Task<string> ReturnValueTest2()
{
await Task.Delay(2000);
Console.WriteLine(string.Format("2s后 ReturnValueTest2 :current thread id----{0}", Thread.CurrentThread.ManagedThreadId));
return "ReturnValueTest2 hello";
}
View Code
CancellationTokenSource类
取消任务
private CancellationTokenSource cancelToken;
private void btnStart2_Click(object sender, RoutedEventArgs e)
{
btnStart2.IsEnabled = false;
btnCancel.IsEnabled = true;
lblLoading.Visibility = Visibility.Visible;
//var t2 = Task.Run(() => work2());
//var awaiter2 = t2.GetAwaiter();
//awaiter2.OnCompleted(() =>
//{
// string message = awaiter2.GetResult();
// txtBox2.Text = message;
//});
//Task<string> t = Task.Run(() => work2()).ContinueWith(x=> txtBox2.Text = x.Result, _syncContextTaskScheduler);
try
{
cancelToken = new CancellationTokenSource();
Task t = Task.Run(() => this.Dispatcher.BeginInvoke(new Action(() =>
{
for (int i = 0; i < 50; i++)
{
ShowMessage2Async(i);
}
}), null), cancelToken.Token);
t.GetAwaiter().OnCompleted(() =>
{
btnStart2.IsEnabled = true;
lblLoading.Visibility = Visibility.Hidden;
});
}
//throw new OperationCanceledException();
catch (OperationCanceledException)
{
txtBox2.Text = "cancel operation";
}
//Task.Factory.StartNew(() =>
//{
// this.Dispatcher.BeginInvoke(new Action(ShowMessageAsync2), null);
//});
//Task.Run(() =>
//{
// this.Dispatcher.BeginInvoke(new Action(ShowMessageAsync2), null);
// //this.Dispatcher.Invoke(() =>
// //{
// // ShowMessageAsync2();
// //}, DispatcherPriority.Normal);
//});
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
if (cancelToken != null) cancelToken.Cancel();
btnCancel.IsEnabled = false;
}
很多时候,除了像上例中的那样手动取消外,我们往往也要对任务设置一个预期执行时间,对超时的任务自动取消。之前一般做法是新启动一个计时器,在计时器的超时回调中执行CancellationTokenSource.Cancel方法。在.Net 4.5中,该操作得到了进一步的简化,我们可以通过在创建CancellationTokenSource时设置超时来实现这一功能。
var cancelTokenSource = new CancellationTokenSource(3000);
除此之外,也可以通过如下代码实现同样的效果。
cancelTokenSource.CancelAfter(3000);
PS:WPF 中使用一个专用的 UI 线程来完成界面的操作和更新,这个线程会关联一个唯一的 Dispatcher 对象,用于调度按优先顺序排列的工作项队列。
与 Dispatcher 调度对象想对应的就是 DispatcherObject,在 WPF 中绝大部分控件都继承自 DispatcherObject,甚至包括 Application。这些继承自 DispatcherObject 的对象具有线程关联特征,也就意味着只有创建这些对象实例,且包含了 Dispatcher 的线程(通常指默认 UI 线程)才能直接对其进行更新操作。