[C#] 搜索文件(多线程并行执行) →→→→→进入此内容的聊天室

来自 , 2020-05-11, 写在 C#, 查看 98 次.
URL http://www.code666.cn/view/2288f691
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. class Program
  6. {
  7.         static void Main()
  8.         {
  9.                 // Return a value type with a lambda expression
  10.                 Task task1 = Task.Factory.StartNew ( () => 1 );
  11.                 int i = task1.Result;
  12.                 // Return a named reference type with a multi-line statement lambda.
  13.                 Task task2 = Task.Factory.StartNew ( () =>
  14.                 {
  15.                         string s = ".NET";
  16.                         double d = 4.0;
  17.                         return new Test { Name = s, Number = d };
  18.                 } );
  19.                 Test test = task2.Result;
  20.                 // Return an array produced by a PLINQ query
  21.                 Task task3 = Task.Factory.StartNew ( () =>
  22.                 {
  23.                         string path = @"C:\users\public\pictures\";
  24.                         string[] files = Directory.GetFiles(path);
  25.                         var result = (from file in files.AsParallel()
  26.                         let info = new FileInfo(file)
  27.                         where info.Extension == ".jpg"
  28.                         select file).ToArray();
  29.                         return result;
  30.                 });
  31.                         foreach (var name in task3.Result)
  32.                         Console.WriteLine(name);
  33.                 }
  34.                         class Test
  35.                         {
  36.                         public string Name { get; set; }
  37.                         public double Number { get; set; }
  38.                 }
  39.                 }
  40.  

回复 "搜索文件(多线程并行执行)"

这儿你可以回复上面这条便签

captcha