楼主: Lisrelchen
2129 3

.NET 4.5 Parallel Extensions Cookbook [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-8-16 09:09:04 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

  • .NET 4.5 Parallel Extensions Cookbook
  • By: Bryan Freeman

  • Publisher: Packt Publishing

  • Pub. Date: July 26, 2013

  • Print ISBN-13: 978-1-84969-022-5

  • Web ISBN-13: 978-1-84969-023-2

  • Pages in Print Edition: 336

  • Subscriber Rating: [0 Ratings]




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Extensions Extension Parallel Cookbook Paralle

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2015-8-16 09:13:29
Creating a task

Tasks are an abstraction in the .NET framework to represent asynchronous units of work. In some ways, a task resembles the creation of a classic .NET thread, but provides a higher level of abstraction,which makes your code easier to write and read.

We will look at the three basic ways to create and run a new task.

  • The Parallel.Invoke() method: This method provides an easy way to run any number of concurrent statements
  • The Task.Start() method: This method starts a task and schedules it for execution with TaskScheduler
  • The Task.Factory.StartNew() method: This method creates and starts a task using Task.Factory

In this recipe, we will create a new task using each of these three methods. To give our tasks something to do, we will be using WebClient to read the text of three classic books. We will then split the words of each book into a string array, and display a count of the words in each book.

How to do it…

Ok, let's start building a Console application that demonstrates the various ways to create a parallel task.

  • Launch Visual Studio 2012.
  • Start a new project using the C# Console Application project template, and assign SimpleTasks as the Solution name as shown in the following screenshot:
  • Add the following using statements at the top of your Program class:
    1. using System;
    2. using System.Linq;
    3. using System.Net;
    4. using System.Threading.Tasks
    复制代码


    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visithttp://www.packtpub.com/support and register to have the files e-mailed directly to you.



  • First, let's create a task using Parallel.Invoke. Add the following code to the Main method of the Program class:
    1. char[] delimiters = { ' ', ',', '.', ';', ':', '-', '_', '/', '\u000A' };
    2. const string headerText = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";

    3. Parallel.Invoke(() =>
    4.     {
    5.     Console.WriteLine("Starting first task using Parallel.Invoke");
    6.     var client = new WebClient();
    7.     client.Headers.Add("user-agent", headerText);
    8.     var words =client.DownloadString(@"http://www.gutenberg.org/files/2009/2009.txt");
    9.     var wordArray = words.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    10.     Console.WriteLine("Origin of Species word count: {0}", wordArray.Count());
    11.     client.Dispose();
    12.     }
    13. );
    复制代码


  • Next, let's start task using the Start method of the Task object. Add the following code to the Main method of the Program class just below the code for the previous step:
    1. var secondTask = new Task(() =>
    2.     {
    3.     Console.WriteLine("Starting second task using Task.Start");
    4.     var client = new WebClient();
    5.     client.Headers.Add("user-agent", headerText);
    6.     var words = client.DownloadString(@"http://www.gutenberg.org/files/16328/16328-8.txt");
    7.     var wordArray = words.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    8.     Console.WriteLine("Beowulf word count: {0}", wordArray.Count());
    9.     client.Dispose();
    10.     }
    11.   );
    12. secondTask.Start();
    复制代码


  • Finally, let's create task using Task.Factory.StartNew. Add the following code to the Main method of the Program class:
    1. Task.Factory.StartNew(() =>
    2.     {
    3.     Console.WriteLine("Starting third task using Task.Factory.StartNew");
    4.     var client = new WebClient();
    5.     client.Headers.Add("user-agent", headerText);
    6.     var words = client.DownloadString(@"http://www.gutenberg.org/files/4300/4300.txt");
    7.     var wordArray = words.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    8.     Console.WriteLine("Ulysses word count: {0}", wordArray.Count());
    9.     client.Dispose();
    10.     }
    11. );
    12. //wait for Enter key to exit
    13. Console.ReadLine();
    复制代码


  • In Visual Studio 2012, press F5 to run the project. You should see output similar to the following screenshot. Note that the exact order of the text you see may vary as tasks run asynchronously:




藤椅
mipbuilder 发表于 2015-8-16 11:40:50
PacktPub.NET.4.5.Parallel.Extensions.Jul.2013_001.png 2_003.png

PacktPub.NET.4.5.Parallel.Extensions.Jul.2013.pdf (3.12 MB)
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
Nicolle + 100 + 100 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 100  论坛币 + 100  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

板凳
ReneeBK 发表于 2015-8-30 02:28:58
mipbuilder 发表于 2015-8-16 11:40

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-30 00:58