TPL——开始一个Task
来源:互联网 发布:淘宝网运动鞋女 编辑:程序博客网 时间:2024/05/20 07:53
创建Tasks的四个方式:
using System;using System.Threading.Tasks;namespace Listing_02 {class Listing_02 {static void Main(string[] args) {// use an Action delegate and a named methodTask task1 = new Task(new Action(printMessage));// use a anonymous delegateTask task2 = new Task(delegate {printMessage();});// use a lambda expression and a named methodTask task3 = new Task(() => printMessage());// use a lambda expression and an anonymous methodTask task4 = new Task(() => {printMessage();});task1.Start();task2.Start();task3.Start();task4.Start();// wait for input before exitingConsole.WriteLine("Main method complete. Press enter to finish.");Console.ReadLine();}static void printMessage() {Console.WriteLine("Hello World");}}}创建并且运行Tasks
Task.Factory.StartNew(() => {Console.WriteLine("Hello World");});
传递参数
using System;using System.Threading.Tasks;namespace Listing_03 {class Listing_03 {static void Main(string[] args) {// use an Action delegate and a named methodTask task1 = new Task(new Action<object>(printMessage),"First task");// use a anonymous delegateTask task2 = new Task(delegate (object obj) {printMessage(obj);}, "Second Task");// use a lambda expression and a named method// note that parameters to a lambda don’t need// to be quoted if there is only one parameterTask task3 = new Task((obj) => printMessage(obj), "Third task");// use a lambda expression and an anonymous methodTask task4 = new Task((obj) => {printMessage(obj);}, "Fourth task");task1.Start();task2.Start();task3.Start();task4.Start();// wait for input before exitingConsole.WriteLine("Main method complete. Press enter to finish.");Console.ReadLine();}static void printMessage(object message) {Console.WriteLine("Message: {0}", message);}}}
返回结果
using System;using System.Threading.Tasks;namespace Listing_05 {class Listing_05 {static void Main(string[] args) {// create the taskTask<int> task1 = new Task<int>(() => {int sum = 0;for (int i = 0; i < 100; i++) {sum += i;}return sum;});// start the tasktask1.Start();// write out the resultConsole.WriteLine("Result 1: {0}", task1.Result);// create the task using stateTask<int> task2 = new Task<int>(obj => {int sum = 0;int max = (int)obj;for (int i = 0; i < max; i++) {sum += i;}return sum;}, 100);// start the tasktask2.Start();// write out the resultConsole.WriteLine("Result 2: {0}", task2.Result);// wait for input before exitingConsole.WriteLine("Main method complete. Press enter to finish.");Console.ReadLine();}}}在取得结果处,线程会阻塞直至结果返回。
0 0
- TPL——开始一个Task
- TPL——取消一个Task
- TPL——等待Task
- TPL——Task的状态及堕性Task
- 【C#】43. TPL基础——Task初步
- Task Parallel Library ( TPL)学习
- TPL Part 1 Task 的使用
- TPL Part 4 -- Task的协同
- 11-14的代码子,for tpl variable——modifier。tpl 你们懂得~
- TPL
- 微软最新的线程编程方式TPL(Task Paraller Library)
- C# 异步编程3 TPL Task 异步程序开发
- C# 异步编程3 TPL Task 异步程序开发
- 一个C的序列化库tpl
- 一个TASK
- .NET(C#) TPL:Task中未觉察异常和TaskScheduler.UnobservedTaskException事件
- 一个新的开始——2014001
- Android总结——开始一个Activity
- 递归算法的优化思路和CPS
- iframe加载顺序导致数据访问出现问题
- leetcode : jumpgame
- MAC地址和IP地址
- TYVJ1415 差分约束
- TPL——开始一个Task
- nodejs操作MSSQL两种方式--笔记
- 设计模式学习笔记——观察者模式
- pat1014 Waiting in line
- 357. Count Numbers with Unique Digits
- java运行顺序
- ubuntu 14.04 编译android6.01 (freescale-imx6)
- 什么是经验贝叶斯克里金插值法?
- Leetcode:Find the Difference