标签:factory 使用 基于 oop ros run book running ble
----------------------------------------------------------------定义 ----------------------------------------------------------------
Object,派生:
System.Threading.Tasks.Task<TResult>,实现:
IAsyncResult IDisposable
Task类表示不返回值并且通常以异步方式执行的单个操作。 Task 对象是在 .NET Framework 4 中首次引入的 基于任务的异步模式 的中心组件之一。 由于对象执行的工作 Task 通常在线程池线程上异步执行,而不是在主应用程序线程上同步执行,因此可以使用 Status 属性以及 IsCanceled 、 IsCompleted 和 IsFaulted 属性来确定任务的状态。 通常,lambda 表达式用于指定任务要执行的工作。
对于返回值的操作,请使用 Task<TResult> 类。
----------------------------------------------------------------任务实例化示例---------------------------------------------------------------using System; using System.Threading; using System.Threading.Tasks; class Example { static void Main() { Action<object> action = (object obj) => { Console.WriteLine("Task={0}, obj={1}, Thread={2}", Task.CurrentId, obj, Thread.CurrentThread.ManagedThreadId); }; // Create a task but do not start it. Task t1 = new Task(action, "alpha"); // Construct a started task Task t2 = Task.Factory.StartNew(action, "beta"); // Block the main thread to demonstrate that t2 is executing t2.Wait(); // Launch t1 t1.Start(); Console.WriteLine("t1 has been launched. (Main Thread={0})", Thread.CurrentThread.ManagedThreadId); // Wait for the task to finish. t1.Wait(); // Construct a started task using Task.Run. String taskData = "delta"; Task t3 = Task.Run( () => {Console.WriteLine("Task={0}, obj={1}, Thread={2}", Task.CurrentId, taskData, Thread.CurrentThread.ManagedThreadId); }); // Wait for the task to finish. t3.Wait(); // Construct an unstarted task Task t4 = new Task(action, "gamma"); // Run it synchronously t4.RunSynchronously(); // Although the task was run synchronously, it is a good practice // to wait for it in the event exceptions were thrown by the task. t4.Wait(); } } // The example displays output like the following: // Task=1, obj=beta, Thread=3 // t1 has been launched. (Main Thread=1) // Task=2, obj=alpha, Thread=4 // Task=3, obj=delta, Thread=3 // Task=4, obj=gamma, Thread=1
using System; using System.Threading.Tasks; public class Example { public static async Task Main() { await Task.Run( () => { // Just loop. int ctr = 0; for (ctr = 0; ctr <= 1000000; ctr++) {} Console.WriteLine("Finished {0} loop iterations", ctr); } ); } } // The example displays the following output: // Finished 1000001 loop iterations
using System; using System.Threading; using System.Threading.Tasks; class Program { static Random rand = new Random(); static void Main() { // Wait on a single task with no timeout specified. Task taskA = Task.Run( () => Thread.Sleep(2000)); Console.WriteLine("taskA Status: {0}", taskA.Status); try { taskA.Wait(); Console.WriteLine("taskA Status: {0}", taskA.Status); } catch (AggregateException) { Console.WriteLine("Exception in taskA."); } } } // The example displays output like the following: // taskA Status: WaitingToRun // taskA Status: RanToCompletion
using System; using System.Threading; using System.Threading.Tasks; public class Example { public static void Main() { // Wait on a single task with a timeout specified. Task taskA = Task.Run( () => Thread.Sleep(2000)); try { taskA.Wait(1000); // Wait for 1 second. bool completed = taskA.IsCompleted; Console.WriteLine("Task A completed: {0}, Status: {1}", completed, taskA.Status); if (! completed) Console.WriteLine("Timed out before task A completed."); } catch (AggregateException) { Console.WriteLine("Exception in taskA."); } } } // The example displays output like the following: // Task A completed: False, Status: Running // Timed out before task A completed.
using System; using System.Threading; using System.Threading.Tasks; public class Example { public static void Main() { var tasks = new Task[3]; var rnd = new Random(); for (int ctr = 0; ctr <= 2; ctr++) tasks[ctr] = Task.Run( () => Thread.Sleep(rnd.Next(500, 3000))); try { int index = Task.WaitAny(tasks); Console.WriteLine("Task #{0} completed first.\n", tasks[index].Id); Console.WriteLine("Status of all tasks:"); foreach (var t in tasks) Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status); } catch (AggregateException) { Console.WriteLine("An exception occurred."); } } } // The example displays output like the following: // Task #1 completed first. // // Status of all tasks: // Task #3: Running // Task #1: RanToCompletion // Task #4: Running
using System; using System.Threading; using System.Threading.Tasks; public class Example { public static void Main() { // Wait for all tasks to complete. Task[] tasks = new Task[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.Run(() => Thread.Sleep(2000)); } try { Task.WaitAll(tasks); } catch (AggregateException ae) { Console.WriteLine("One or more exceptions occurred: "); foreach (var ex in ae.Flatten().InnerExceptions) Console.WriteLine(" {0}", ex.Message); } Console.WriteLine("Status of completed tasks:"); foreach (var t in tasks) Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status); } } // The example displays the following output: // Status of completed tasks: // Task #2: RanToCompletion // Task #1: RanToCompletion // Task #3: RanToCompletion // Task #4: RanToCompletion // Task #6: RanToCompletion // Task #5: RanToCompletion // Task #7: RanToCompletion // Task #8: RanToCompletion // Task #9: RanToCompletion // Task #10: RanToCompletion
标签:factory 使用 基于 oop ros run book running ble
原文地址:https://www.cnblogs.com/HomeSapiens/p/14065628.html