---
title: "多线程并行搜索文件"
author: "devgis"
date: "2012-11-16T10:45:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2012-11-21T09:19:27Z"
id: 1731473
url: "https://devgis.iteye.com/blog/1731473"
---

`usingSystem;`

`02` `usingSystem.IO;`

`03` `usingSystem.Linq;`

`04` `usingSystem.Threading.Tasks;`

`05`

`06` `classProgram`

`07` `{`

`08` `staticvoidMain()`

`09` `{`

`10` `// Return a value type with a lambda expression`

`11` `Task<int> task1 = Task<int>.Factory.StartNew(() => 1);`

`12` `inti = task1.Result;`

`13`

`14` `// Return a named reference type with a multi-line statement lambda.`

`15` `Task<Test> task2 = Task<Test>.Factory.StartNew(() =>`

`16` `{`

`17` `strings =".NET";`

`18` `doubled = 4.0;`

`19` `returnnewTest { Name = s, Number = d };`

`20` `});`

`21` `Test test = task2.Result;`

`22`

`23` `// Return an array produced by a PLINQ query`

`24` `Task<string[]> task3 = Task<string[]>.Factory.StartNew(() =>`

`25` `{`

`26` `stringpath =@"C:\users\public\pictures\";`

`27` `string[] files = Directory.GetFiles(path);`

`28`

`29` `var result = (from fileinfiles.AsParallel()`

`30` `let info =newFileInfo(file)`

`31` `where info.Extension ==".jpg"`

`32` `select file).ToArray();`

`33`

`34` `returnresult;`

`35` `});`

`36`

`37` `foreach(var nameintask3.Result)`

`38` `Console.WriteLine(name);`

`39`

`40` `}`

`41` `classTest`

`42` `{`

`43` `publicstringName {get;set; }`

`44` `publicdoubleNumber {get;set; }`

`45`

`46` `}`

`47` `}`