当前位置: 首页 > news >正文

辽宁省建设厅科技中心网站群晖手动安装wordpress

辽宁省建设厅科技中心网站,群晖手动安装wordpress,上海网站建设 销售,php源码怎么搭建网站本文内容 隐式创建和运行任务显式创建和运行任务任务 ID任务创建选项任务、线程和区域性创建任务延续创建分离的子任务创建子任务等待任务完成组合任务处理任务中的异常取消任务TaskFactory 类无委托的任务自定义计划程序相关数据结构自定义任务类型 任务并行库 (TPL) 以“任…本文内容 隐式创建和运行任务显式创建和运行任务任务 ID任务创建选项任务、线程和区域性创建任务延续创建分离的子任务创建子任务等待任务完成组合任务处理任务中的异常取消任务TaskFactory 类无委托的任务自定义计划程序相关数据结构自定义任务类型 任务并行库 (TPL) 以“任务”的概念为基础后者表示异步操作。 在某些方面任务类似于线程或 ThreadPool 工作项但是抽象级别更高。 术语“任务并行”是指一个或多个独立的任务同时运行。 任务提供两个主要好处 系统资源的使用效率更高可伸缩性更好。 在后台任务排队到已使用算法增强的 ThreadPool这些算法能够确定线程数并随之调整。 这些算法提供负载平衡以实现吞吐量最大化。 此进程会使任务相对轻量你可以创建很多任务以启用细化并行。 对于线程或工作项可以使用更多的编程控件。 任务和围绕它们生成的框架提供了一组丰富的 API这些 API 支持等待、取消、继续、可靠的异常处理、详细状态、自定义计划等功能。 出于这两个原因在 .NET 中TPL 是用于编写多线程、异步和并行代码的首选 API。 1、隐式创建和运行任务 Parallel.Invoke 方法提供了一种简便方式可同时运行任意数量的任意语句。 只需为每个工作项传入 Action 委托即可。 创建这些委托的最简单方式是使用 lambda 表达式。 lambda 表达式可调用指定的方法或提供内联代码。 下面的示例演示一个基本的 Invoke 调用该调用创建并启动同时运行的两个任务。 第一个任务由调用名为 DoSomeWork 的方法的 lambda 表达式表示第二个任务由调用名为 DoSomeOtherWork 的方法的 lambda 表达式表示。 备注 本文档使用 lambda 表达式在 TPL 中定义委托。  Parallel.Invoke(() DoSomeWork(), () DoSomeOtherWork());备注 Invoke 在后台创建的 Task 实例数不一定与所提供的委托数相等。 TPL 可能会使用各种优化特别是对于大量的委托。 为了更好地控制任务执行或从任务返回值必须更加显式地使用 Task 对象。 2、显式创建和运行任务 不返回值的任务由 System.Threading.Tasks.Task 类表示。 返回值的任务由 System.Threading.Tasks.TaskTResult 类表示该类从 Task 继承。 任务对象处理基础结构详细信息并提供可在任务的整个生存期内从调用线程访问的方法和属性。 例如可以随时访问任务的 Status 属性以确定它是已开始运行、已完成运行、已取消还是引发了异常。 状态由 TaskStatus 枚举表示。 在创建任务时你赋予它一个用户委托该委托封装该任务将执行的代码。 该委托可以表示为命名的委托、匿名方法或 lambda 表达式。 lambda 表达式可以包含对命名方法的调用如下面的示例所示。 该示例包含对 Task.Wait 方法的调用以确保任务在控制台模式应用程序结束之前完成执行。 using System; using System.Threading; using System.Threading.Tasks;public class Lambda {public static void Main(){Thread.CurrentThread.Name Main;// Create a task and supply a user delegate by using a lambda expression.Task taskA new Task( () Console.WriteLine(Hello from taskA.));// Start the task.taskA.Start();// Output a message from the calling thread.Console.WriteLine(Hello from thread {0}.,Thread.CurrentThread.Name);taskA.Wait();} } // The example displays output as follows: // Hello from thread Main. // Hello from taskA. // or // Hello from taskA. // Hello from thread Main.你还可以使用 Task.Run 方法通过一个操作创建并启动任务。 无论是哪个任务计划程序与当前线程关联Run 方法都将使用默认的任务计划程序来管理任务。 不需要对任务的创建和计划进行更多控制时首选 Run 方法创建并启动任务。 using System; using System.Threading; using System.Threading.Tasks;namespace Run;public class Example {public static void Main(){Thread.CurrentThread.Name Main;// Define and run the task.Task taskA Task.Run( () Console.WriteLine(Hello from taskA.));// Output a message from the calling thread.Console.WriteLine(Hello from thread {0}.,Thread.CurrentThread.Name);taskA.Wait();} } // The example displays output as follows: // Hello from thread Main. // Hello from taskA. // or // Hello from taskA. // Hello from thread Main.你还可以使用 TaskFactory.StartNew 方法在一个操作中创建并启动任务。 如下例所示可以在以下情况下使用此方法 无需将创建与计划分开且需要额外的任务创建选项或需要使用特定的计划程序。 需要将其他状态传递给可通过其 Task.AsyncState 属性检索的任务。 using System; using System.Threading; using System.Threading.Tasks;namespace TaskIntro;class CustomData {public long CreationTime;public int Name;public int ThreadNum; }public class AsyncState {public static void Main(){Task[] taskArray new Task[10];for (int i 0; i taskArray.Length; i){taskArray[i] Task.Factory.StartNew((Object obj) {CustomData data obj as CustomData;if (data null) return;data.ThreadNum Thread.CurrentThread.ManagedThreadId;},new CustomData() { Name i, CreationTime DateTime.Now.Ticks });}Task.WaitAll(taskArray);foreach (var task in taskArray){var data task.AsyncState as CustomData;if (data ! null)Console.WriteLine(Task #{0} created at {1}, ran on thread #{2}.,data.Name, data.CreationTime, data.ThreadNum);}} } // The example displays output like the following: // Task #0 created at 635116412924597583, ran on thread #3. // Task #1 created at 635116412924607584, ran on thread #4. // Task #2 created at 635116412924607584, ran on thread #4. // Task #3 created at 635116412924607584, ran on thread #4. // Task #4 created at 635116412924607584, ran on thread #3. // Task #5 created at 635116412924607584, ran on thread #3. // Task #6 created at 635116412924607584, ran on thread #4. // Task #7 created at 635116412924607584, ran on thread #4. // Task #8 created at 635116412924607584, ran on thread #3. // Task #9 created at 635116412924607584, ran on thread #4.Task 和 TaskTResult 均公开静态 Factory 属性该属性返回 TaskFactory 的默认实例因此你可以调用该方法为 Task.Factory.StartNew()。 此外在以下示例中由于任务的类型为 System.Threading.Tasks.TaskTResult因此每个任务都具有包含计算结果的公共 TaskTResult.Result 属性。 任务以异步方式运行可以按任意顺序完成。 如果在计算完成之前访问 Result 属性则该属性将阻止调用线程。 使用 lambda 表达式创建委托时你有权访问源代码中当时可见的所有变量。 然而在某些情况下特别是在循环中lambda 不按照预期的方式捕获变量。 它仅捕获变量的引用而不是它每次迭代后更改的值。 以下示例演示了该问题。 它将循环计数器传递给实例化 CustomData 对象并使用循环计数器作为对象标识符的 lambda 表达式。 如示例输出所示每个 CustomData 对象都具有相同的标识符。 using System; using System.Threading; using System.Threading.Tasks;namespace Example.Iterations;class CustomData {public long CreationTime;public int Name;public int ThreadNum; }public class IterationTwo {public static void Main(){// Create the task object by using an Action(Of Object) to pass in the loop// counter. This produces an unexpected result.Task[] taskArray new Task[10];for (int i 0; i taskArray.Length; i) {taskArray[i] Task.Factory.StartNew( (Object obj) {var data new CustomData() {Name i, CreationTime DateTime.Now.Ticks};data.ThreadNum Thread.CurrentThread.ManagedThreadId;Console.WriteLine(Task #{0} created at {1} on thread #{2}.,data.Name, data.CreationTime, data.ThreadNum);},i );}Task.WaitAll(taskArray);} } // The example displays output like the following: // Task #10 created at 635116418427727841 on thread #4. // Task #10 created at 635116418427737842 on thread #4. // Task #10 created at 635116418427737842 on thread #4. // Task #10 created at 635116418427737842 on thread #4. // Task #10 created at 635116418427737842 on thread #4. // Task #10 created at 635116418427737842 on thread #4. // Task #10 created at 635116418427727841 on thread #3. // Task #10 created at 635116418427747843 on thread #3. // Task #10 created at 635116418427747843 on thread #3. // Task #10 created at 635116418427737842 on thread #4.通过使用构造函数向任务提供状态对象可以在每次迭代时访问该值。 以下示例在上一示例的基础上做了修改在创建 CustomData 对象时使用循环计数器该对象继而传递给 lambda 表达式。 如示例输出所示每个 CustomData 对象现在都具有唯一的一个标识符该标识符基于该对象实例化时循环计数器的值。 using System; using System.Threading; using System.Threading.Tasks;class CustomData {public long CreationTime;public int Name;public int ThreadNum; }public class IterationOne {public static void Main(){// Create the task object by using an Action(Of Object) to pass in custom data// to the Task constructor. This is useful when you need to capture outer variables// from within a loop.Task[] taskArray new Task[10];for (int i 0; i taskArray.Length; i) {taskArray[i] Task.Factory.StartNew( (Object obj ) {CustomData data obj as CustomData;if (data null)return;data.ThreadNum Thread.CurrentThread.ManagedThreadId;Console.WriteLine(Task #{0} created at {1} on thread #{2}.,data.Name, data.CreationTime, data.ThreadNum);},new CustomData() {Name i, CreationTime DateTime.Now.Ticks} );}Task.WaitAll(taskArray);} } // The example displays output like the following: // Task #0 created at 635116412924597583 on thread #3. // Task #1 created at 635116412924607584 on thread #4. // Task #3 created at 635116412924607584 on thread #4. // Task #4 created at 635116412924607584 on thread #4. // Task #2 created at 635116412924607584 on thread #3. // Task #6 created at 635116412924607584 on thread #3. // Task #5 created at 635116412924607584 on thread #4. // Task #8 created at 635116412924607584 on thread #4. // Task #7 created at 635116412924607584 on thread #3. // Task #9 created at 635116412924607584 on thread #4.此状态作为参数传递给任务委托并且可通过使用 Task.AsyncState 属性从任务对象访问。 以下示例在上一示例的基础上演变而来。 它使用 AsyncState 属性显示关于传递到 lambda 表达式的 CustomData 对象的信息。 using System; using System.Threading; using System.Threading.Tasks;namespace TaskIntro;class CustomData {public long CreationTime;public int Name;public int ThreadNum; }public class AsyncState {public static void Main(){Task[] taskArray new Task[10];for (int i 0; i taskArray.Length; i){taskArray[i] Task.Factory.StartNew((Object obj) {CustomData data obj as CustomData;if (data null) return;data.ThreadNum Thread.CurrentThread.ManagedThreadId;},new CustomData() { Name i, CreationTime DateTime.Now.Ticks });}Task.WaitAll(taskArray);foreach (var task in taskArray){var data task.AsyncState as CustomData;if (data ! null)Console.WriteLine(Task #{0} created at {1}, ran on thread #{2}.,data.Name, data.CreationTime, data.ThreadNum);}} } // The example displays output like the following: // Task #0 created at 635116412924597583, ran on thread #3. // Task #1 created at 635116412924607584, ran on thread #4. // Task #2 created at 635116412924607584, ran on thread #4. // Task #3 created at 635116412924607584, ran on thread #4. // Task #4 created at 635116412924607584, ran on thread #3. // Task #5 created at 635116412924607584, ran on thread #3. // Task #6 created at 635116412924607584, ran on thread #4. // Task #7 created at 635116412924607584, ran on thread #4. // Task #8 created at 635116412924607584, ran on thread #3. // Task #9 created at 635116412924607584, ran on thread #4.3、任务 ID 每个任务都获得一个在应用程序域中唯一标识自己的整数 ID可以使用 Task.Id 属性访问该 ID。 该 ID 可有效用于在 Visual Studio 调试器的“并行堆栈”和“任务”窗口中查看任务信息。 该 ID 是以惰性方式创建的这意味着请求该 ID 时才会创建该 ID。 因此每次运行程序时任务可能具有不同的 ID。  4、任务创建选项 创建任务的大多数 API 提供接受 TaskCreationOptions 参数的重载。 通过指定下列某个或多个选项可指示任务计划程序在线程池中安排任务计划的方式。 可以使用位 OR 运算组合选项。 下面的示例演示一个具有 LongRunning 和 PreferFairness 选项的任务 var task3 new Task(() MyLongRunningMethod(),TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness); task3.Start();5、任务、线程和区域性 每个线程都具有一个关联的区域性和 UI 区域性分别由 Thread.CurrentCulture 和 Thread.CurrentUICulture 属性定义。 线程的区域性用在格式设置、分析、排序和字符串比较等操作中。 线程的 UI 区域性用于查找资源。 系统区域性定义线程的默认区域性和 UI 区域性。 但你可以使用 CultureInfo.DefaultThreadCurrentCulture 和 CultureInfo.DefaultThreadCurrentUICulture 属性为应用程序域中的所有线程指定默认区域性。 如果你显式设置线程的区域性并启动新线程则新线程不会继承正在调用的线程的区域性相反其区域性就是默认系统区域性。 但是在基于任务的编程中任务使用调用线程的区域性即使任务在不同线程上以异步方式运行也是如此。 下面的示例提供了简单的演示。 它将应用的当前区域性更改为法语法国。 如果法语法国已经是当前区域性它会更改为英语美国。 然后调用一个名为 formatDelegate 的委托该委托返回在新区域性中格式化为货币值的数字。 无论委托是由任务同步调用还是异步调用该任务都将使用调用线程的区域性。 using System; using System.Globalization; using System.Threading; using System.Threading.Tasks;public class Example {public static void Main(){decimal[] values { 163025412.32m, 18905365.59m };string formatString C2;FuncString formatDelegate () { string output String.Format(Formatting using the {0} culture on thread {1}.\n,CultureInfo.CurrentCulture.Name,Thread.CurrentThread.ManagedThreadId);foreach (var value in values)output String.Format({0} , value.ToString(formatString));output Environment.NewLine;return output;};Console.WriteLine(The example is running on thread {0},Thread.CurrentThread.ManagedThreadId);// Make the current culture different from the system culture.Console.WriteLine(The current culture is {0},CultureInfo.CurrentCulture.Name);if (CultureInfo.CurrentCulture.Name fr-FR)Thread.CurrentThread.CurrentCulture new CultureInfo(en-US);elseThread.CurrentThread.CurrentCulture new CultureInfo(fr-FR);Console.WriteLine(Changed the current culture to {0}.\n,CultureInfo.CurrentCulture.Name);// Execute the delegate synchronously.Console.WriteLine(Executing the delegate synchronously:);Console.WriteLine(formatDelegate());// Call an async delegate to format the values using one format string.Console.WriteLine(Executing a task asynchronously:);var t1 Task.Run(formatDelegate);Console.WriteLine(t1.Result);Console.WriteLine(Executing a task synchronously:);var t2 new TaskString(formatDelegate);t2.RunSynchronously();Console.WriteLine(t2.Result);} } // The example displays the following output: // The example is running on thread 1 // The current culture is en-US // Changed the current culture to fr-FR. // // Executing the delegate synchronously: // Formatting using the fr-FR culture on thread 1. // 163 025 412,32 € 18 905 365,59 € // // Executing a task asynchronously: // Formatting using the fr-FR culture on thread 3. // 163 025 412,32 € 18 905 365,59 € // // Executing a task synchronously: // Formatting using the fr-FR culture on thread 1. // 163 025 412,32 € 18 905 365,59 €在 .NET Framework 4.6 之前的 .NET Framework 版本中任务的区域性由它在其上运行的线程区域性确定而不是调用线程的区域性。 对于异步任务任务使用的区域性可能不同于调用线程的区域性。 有关异步任务和区域性的详细信息请参阅 CultureInfo 一文中的“区域性和基于异步任务的操作”部分。 6、创建任务延续 使用 Task.ContinueWith 和 TaskTResult.ContinueWith 方法可以指定要在先行任务完成时启动的任务。 延续任务的委托被传递给对先行任务的引用以便它查看先行任务的状态。 通过检索 TaskTResult.Result 属性的值可以将先行任务的输出用作延续任务的输入。 在下面的示例中getData 任务通过调用 TaskFactory.StartNewTResult(FuncTResult) 方法来启动。 当 processData 完成时getData 任务自动启动当 displayData 完成时processData 启动。 getData 产生一个整数数组通过 processData 任务的 getData 属性TaskTResult.Result 任务可访问该数组。 processData 任务处理该数组并返回结果结果的类型从传递到 TaskTResult.ContinueWithTNewResult(FuncTaskTResult,TNewResult) 方法的 Lambda 表达式的返回类型推断而来。 displayData 完成时processData 任务自动执行而 TupleT1,T2,T3 任务可通过 processData 任务的 displayData 属性访问由 processData lambda 表达式返回的 TaskTResult.Result 对象。 displayData 任务采用 processData 任务的结果。 它得出自己的结果其类型以相似方式推断而来且可由程序中的 Result 属性使用。 using System; using System.Threading.Tasks;public class ContinuationOne {public static void Main(){var getData Task.Factory.StartNew(() {Random rnd new Random();int[] values new int[100];for (int ctr 0; ctr values.GetUpperBound(0); ctr)values[ctr] rnd.Next();return values;} );var processData getData.ContinueWith((x) {int n x.Result.Length;long sum 0;double mean;for (int ctr 0; ctr x.Result.GetUpperBound(0); ctr)sum x.Result[ctr];mean sum / (double) n;return Tuple.Create(n, sum, mean);} );var displayData processData.ContinueWith((x) {return String.Format(N{0:N0}, Total {1:N0}, Mean {2:N2},x.Result.Item1, x.Result.Item2,x.Result.Item3);} );Console.WriteLine(displayData.Result);} } // The example displays output similar to the following: // N100, Total 110,081,653,682, Mean 1,100,816,536.82因为 Task.ContinueWith 是实例方法所以你可以将方法调用链接在一起而不是为每个先行任务去实例化 TaskTResult 对象。 以下示例与上一示例在功能上等同唯一的不同在于它将对 Task.ContinueWith 方法的调用链接在一起。 通过方法调用链返回的 TaskTResult 对象是最终延续任务。 using System; using System.Threading.Tasks;public class ContinuationTwo {public static void Main(){var displayData Task.Factory.StartNew(() {Random rnd new Random();int[] values new int[100];for (int ctr 0; ctr values.GetUpperBound(0); ctr)values[ctr] rnd.Next();return values;} ).ContinueWith((x) {int n x.Result.Length;long sum 0;double mean;for (int ctr 0; ctr x.Result.GetUpperBound(0); ctr)sum x.Result[ctr];mean sum / (double) n;return Tuple.Create(n, sum, mean);} ).ContinueWith((x) {return String.Format(N{0:N0}, Total {1:N0}, Mean {2:N2},x.Result.Item1, x.Result.Item2,x.Result.Item3);} );Console.WriteLine(displayData.Result);} } // The example displays output similar to the following: // N100, Total 110,081,653,682, Mean 1,100,816,536.82使用 ContinueWhenAll 和 ContinueWhenAny 方法可以从多个任务继续。 7、创建分离的子任务 如果在任务中运行的用户代码创建一个新任务且未指定 AttachedToParent 选项则该新任务不采用任何特殊方式与父任务同步。 这种不同步的任务类型称为“分离的嵌套任务”或“分离的子任务”。 以下示例展示了创建一个分离子任务的任务 var outer Task.Factory.StartNew(() {Console.WriteLine(Outer task beginning.);var child Task.Factory.StartNew(() {Thread.SpinWait(5000000);Console.WriteLine(Detached task completed.);}); });outer.Wait(); Console.WriteLine(Outer task completed.); // The example displays the following output: // Outer task beginning. // Outer task completed. // Detached task completed.备注 父任务不会等待分离子任务完成。 8、创建子任务 如果任务中运行的用户代码在创建任务时指定了 AttachedToParent 选项新任务就称为父任务的附加子任务。 因为父任务隐式地等待所有附加子任务完成所以你可以使用 AttachedToParent 选项表示结构化的任务并行。 以下示例展示了创建 10 个附加子任务的父任务。 该示例调用 Task.Wait 方法来等待父任务完成。 它不必显式等待附加子任务完成。 using System; using System.Threading; using System.Threading.Tasks;public class Child {public static void Main(){var parent Task.Factory.StartNew(() {Console.WriteLine(Parent task beginning.);for (int ctr 0; ctr 10; ctr) {int taskNo ctr;Task.Factory.StartNew((x) {Thread.SpinWait(5000000);Console.WriteLine(Attached child #{0} completed.,x);},taskNo, TaskCreationOptions.AttachedToParent);}});parent.Wait();Console.WriteLine(Parent task completed.);} } // The example displays output like the following: // Parent task beginning. // Attached child #9 completed. // Attached child #0 completed. // Attached child #8 completed. // Attached child #1 completed. // Attached child #7 completed. // Attached child #2 completed. // Attached child #6 completed. // Attached child #3 completed. // Attached child #5 completed. // Attached child #4 completed. // Parent task completed.父任务可使用 TaskCreationOptions.DenyChildAttach 选项阻止其他任务附加到父任务。  9、等待任务完成 System.Threading.Tasks.Task 和 System.Threading.Tasks.TaskTResult 类型提供了 Task.Wait 方法的若干重载以便能够等待任务完成。 此外使用静态 Task.WaitAll 和 Task.WaitAny 方法的重载可以等待一批任务中的任一任务或所有任务完成。 通常会出于以下某个原因等待任务 主线程依赖于任务计算的最终结果。 你必须处理可能从任务引发的异常。 应用程序可以在所有任务执行完毕之前终止。 例如执行 Main应用程序入口点中的所有同步代码后控制台应用程序将终止。 下面的示例演示不包含异常处理的基本模式 Task[] tasks new Task[3] {Task.Factory.StartNew(() MethodA()),Task.Factory.StartNew(() MethodB()),Task.Factory.StartNew(() MethodC()) };//Block until all tasks complete. Task.WaitAll(tasks);// Continue on this thread...某些重载允许你指定超时而其他重载采用额外的 CancellationToken 作为输入参数以便可以通过编程方式或根据用户输入来取消等待。 等待任务时其实是在隐式等待使用 TaskCreationOptions.AttachedToParent 选项创建的该任务的所有子级。 Task.Wait 在该任务已完成时立即返回。 Task.Wait 方法将抛出由某任务引发的任何异常即使 Task.Wait 方法是在该任务完成之后调用的。 10、组合任务 Task 和 TaskTResult 类提供了多种方法来帮助组合多个任务。 这些方法实现常用模式并更好地利用 C#、Visual Basic 和 F# 提供的异步语言功能。 本节介绍了 WhenAll、WhenAny、Delay 和 FromResult 方法。 Task.WhenAll Task.WhenAll 方法异步等待多个 Task 或 TaskTResult 对象完成。 通过它提供的重载版本可以等待非均匀任务组。 例如你可以等待多个 Task 和 TaskTResult 对象在一个方法调用中完成。 Task.WhenAny Task.WhenAny 方法异步等待多个 Task 或 TaskTResult 对象中的一个完成。 与在 Task.WhenAll 方法中一样该方法提供重载版本让你能等待非均匀任务组。 WhenAny 方法在下列情境中尤其有用 冗余运算请考虑可以用多种方式执行的算法或运算。 你可使用 WhenAny 方法来选择先完成的运算然后取消剩余的运算。 交错运算你可启动必须完成的多项运算并使用 WhenAny 方法在每项运算完成时处理结果。 在一项运算完成后可以启动一个或多个任务。 限制运算你可使用 WhenAny 方法通过限制并发运算的数量来扩展前面的情境。 到期运算你可使用 WhenAny 方法在一个或多个任务与特定时间后完成的任务例如 Delay 方法返回的任务间进行选择。 下节描述了 Delay 方法。 Task.Delay Task.Delay 方法将生成在指定时间后完成的 Task 对象。 你可以使用此方法生成用于轮询数据的循环指定超时延迟处理用户输入等。 Task(T).FromResult 通过使用 Task.FromResult 方法你可以创建包含预计算结果的 TaskTResult 对象。 执行返回 TaskTResult 对象的异步运算且已计算该 TaskTResult 对象的结果时此方法将十分有用。 11、处理任务中的异常 当某个任务抛出一个或多个异常时异常包装在 AggregateException 异常中。 该异常会传播回与任务联接的线程。 通常该线程是等待任务完成的线程或访问 Result 属性的线程。 此行为强制实施 .NET Framework 策略 - 默认所有未处理的异常应终止进程。 调用代码可以通过使用 try/catch 块中的以下任意方法来处理异常 Wait 方法 WaitAll 方法 WaitAny 方法 Result 属性 联接线程也可以通过在对任务进行垃圾回收之前访问 Exception 属性来处理异常。 通过访问此属性可防止未处理的异常在对象完成时触发终止进程的异常传播行为。 12、取消任务 Task 类支持协作取消并与 .NET Framework 4 中新增的 System.Threading.CancellationTokenSource 类和 System.Threading.CancellationToken 类完全集成。 System.Threading.Tasks.Task 类中的大多数构造函数采用 CancellationToken 对象作为输入参数。 许多 StartNew 和 Run 重载还包括 CancellationToken 参数。 你可以创建标记并使用 CancellationTokenSource 类在以后某一时间发出取消请求。 可以将该标记作为参数传递给 Task还可以在执行响应取消请求的工作的用户委托中引用同一标记。 13、TaskFactory 类 TaskFactory 类提供静态方法这些方法封装了用于创建和启动任务和延续任务的常用模式。 最常用模式为 StartNew它在一个语句中创建并启动任务。 如果通过多个先行任务创建延续任务请使用 ContinueWhenAll 方法或 ContinueWhenAny 方法或它们在 TaskTResult 类中的相当方法。。 若要在 BeginX 或 EndX 实例中封装异步编程模型 Task 和 TaskTResult 方法请使用 FromAsync 方法。 默认的 TaskFactory 可作为 Task 类或 TaskTResult 类上的静态属性访问。 你还可以直接实例化 TaskFactory 并指定各种选项包括 CancellationToken、TaskCreationOptions 选项、TaskContinuationOptions 选项或 TaskScheduler。 创建任务工厂时所指定的任何选项将应用于它创建的所有任务除非 Task 是通过使用 TaskCreationOptions 枚举创建的在这种情况下任务的选项重写任务工厂的选项。 14、无委托的任务 在某些情况下可能需要使用 Task 封装由外部组件而不是用户委托执行的某个异步操作。 如果该操作基于异步编程模型 Begin/End 模式你可以使用 FromAsync 方法。 如果不是这种情况你可以使用 TaskCompletionSourceTResult 对象将该操作包装在任务中并因而获得 Task 可编程性的一些好处。 例如对异常传播和延续的支持。  15、自定义计划程序 大多数应用程序或库开发人员并不关心任务在哪个处理器上运行、任务如何将其工作与其他任务同步以及如何在 System.Threading.ThreadPool 中计划任务。 他们只需要它在主机上尽可能高效地执行。 如果需要对计划细节进行更细化的控制可以使用 TPL 在默认任务计划程序上配置一些设置甚至是提供自定义计划程序。  16、相关数据结构 TPL 有几种在并行和顺序方案中都有用的新公共类型。 其中包括 System.Collections.Concurrent 命名空间中的多个线程安全、快速且可缩放的集合类以及多个新的同步类型。 例如 System.Threading.Semaphore 和 System.Threading.ManualResetEventSlim对于特定类型的工作负载两者在效率方面超过了原有类型。 .NET Framework 4 中的其他新类型例如 System.Threading.Barrier 和 System.Threading.SpinLock提供了早期版本中未提供的功能。  17、自定义任务类型 建议不要从 System.Threading.Tasks.Task 或 System.Threading.Tasks.TaskTResult 继承。 相反我们建议你使用 AsyncState 属性将其他数据或状态与 Task 或 TaskTResult 对象相关联。 还可以使用扩展方法扩展 Task 和 TaskTResult 类的功能。 有关扩展方法的详细信息请参阅扩展方法和扩展方法。 如果必须从 Task 或 TaskTResult 继承则不能使用 Run 或 System.Threading.Tasks.TaskFactorySystem.Threading.Tasks.TaskFactoryTResult 或 System.Threading.Tasks.TaskCompletionSourceTResult 类创建自定义任务类型的实例。 不能使用是因为这些类仅创建 Task 和 TaskTResult 对象。 此外不能使用 Task、TaskTResult、TaskFactory 和 TaskFactoryTResult 提供的任务延续机制创建自定义任务类型的实例。 不能使用也是因为这些类仅创建 Task 和 TaskTResult 对象。
http://www.tj-hxxt.cn/news/221951.html

相关文章:

  • 网页设计与网站建设毕业设计wordpress默认编辑器功能增强
  • h5制作企业网站有哪些优势中国住房和城乡建设部
  • 网站建设情况自查报告建设集团网站 技术支持中企动力
  • 企业建站官网定制网站建设推广方案
  • 中国建设银行大学生招聘信息网站移动端漂亮网站
  • .net可以做网站做游戏 博客园网络营销推广实例
  • 哪个网站可以接项目做免费发帖推广平台
  • 无极app定制开发公司网站模板办公室装修设计效果图大全
  • 苏州网站建设一条龙政务服务网站建设资金
  • 网站备案要网站做才可以使用吗汕头建设银行各支行电话
  • 徐州智能模板建站佳木斯市郊区建设局网站
  • 好的建网站公司成都市微信网站建设报价
  • 公司为什么要网站备案竞价服务托管公司
  • 网站设计的论坛微信h5制作平台
  • 网站自助建设源码企业产品展示型网站案例
  • 定制化网站开发多少钱网站开发技术简介dw
  • 网站开发哪里有直播视频素材
  • 网站建设招标提问资源专业网站优化排名
  • 微商城网站建设咨询南宁代理记账
  • 网站收录优化欧莱雅网站建设与推广方案
  • 德兴网站建设公司美食类网站开发需求分析
  • 做推广适合哪些网站英文网站建设需要准备什么
  • dede 网站建设模板品牌推广服务
  • 南阳网站排名优化价格logo商标设计
  • 微信公众号怎么进行网站建设电脑系统优化软件
  • 义乌购物网站建设多少钱网络营销做女鞋的网站设计
  • 郏县住房和城乡建设局网站引流推广推广微信hyhyk1效果好
  • 济南手机网站四川省信用建设促进会网站
  • 成都金牛网站建设公司建e室内设计
  • 做网站需要些什么网站创作