全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 winbugs及其他软件专版
2081 3
2015-08-16

  • .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]




二维码

扫码加我 拉你入群

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

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

全部回复
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:
    复制代码


    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:
    复制代码


  • 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:
    复制代码


  • Finally, let's create task using Task.Factory.StartNew. Add the following code to the Main method of the Program class:
    复制代码


  • 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:




二维码

扫码加我 拉你入群

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

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

2015-8-16 11:40:50
二维码

扫码加我 拉你入群

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

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

2015-8-30 02:28:58
mipbuilder 发表于 2015-8-16 11:40
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群