标签:status main 创建 complete 已取消 方法 命名 dispatch row
public static void MainThreadPool() { Console.WriteLine("主线程异步操作队列"); ThreadPool.QueueUserWorkItem(ThreadProc); Console.WriteLine("主线程做其他工作"); Thread.Sleep(1000);//模拟其他工作 Console.WriteLine("主线程运行结束"); Console.ReadLine(); } /// <summary> /// 与委托WaitCallback 签名相同的回掉方法 /// </summary> /// <param name="stateInfo">如果使用的是QueueUserWorkItem(WaitCallback callBack),该参数默认为null</param> static void ThreadProc(Object stateInfo) { Console.WriteLine("线程池工作线程执行回掉"); Thread.Sleep(1000); //模拟其他工作 }
输出:
public static void RunGetStartTask() { //创建方式之一 :通过task的构造函数 Task t1 = new Task(Task1); //创建方式之二:通过task工厂调用StartNew 创建并并启用 Task t2 = Task.Factory.StartNew(Task2); Console.WriteLine("t1任务Start之前状态:{0},t2任务状态:{1}", t1.Status, t2.Status); t1.Start(); Console.WriteLine("t1任务Start之后状态:{0},t2任务状态:{1}", t1.Status, t2.Status); Task.WaitAll(t1, t2); Console.WriteLine("主线程执行完毕"); Console.WriteLine("t1最终状态:{0},t2最终状态:{1}", t1.Status, t2.Status); Console.ReadLine(); } public static void Task1() { Thread.Sleep(1000); Console.WriteLine("运行task1"); } public static void Task2() { Thread.Sleep(2000); Console.WriteLine("运行task2"); }
输出
CancellationTokenSource 能够初始化取消的请求,而CancellationToken能够将这些请求传递给异步的操作。
public static void RunGetStartTaskCancel() { var cts = new CancellationTokenSource(); var ct = cts.Token; var sw = Stopwatch.StartNew(); var t1 = Task.Factory.StartNew(() => Task1Cancel(ct), ct); var t2 = Task.Factory.StartNew(() => Task2Cancel(ct), ct); //主线程模拟1秒 Thread.Sleep(1000); cts.Cancel(); try { if (!Task.WaitAll(new Task[] { t1, t2 }, 3000)) { Console.WriteLine("Task1Cancel 和 Task2Cancel 超过了2秒完成."); Console.WriteLine("t1状态" + t1.Status.ToString()); Console.WriteLine("t2状态" + t2.Status.ToString()); Console.ReadLine(); } Console.ReadLine(); } catch (AggregateException ex) { foreach (Exception innerException in ex.InnerExceptions) { Debug.WriteLine(innerException.ToString()); Console.WriteLine("异常消息:" + innerException.ToString()); } if (t1.IsCanceled) { Console.WriteLine("t1 task 运行Task1Cancel 已取消"); } if (t1.IsCanceled) { Console.WriteLine("t2 task 运行Task2Cancel 已取消"); } Console.WriteLine("耗时" + sw.Elapsed.ToString()); Console.WriteLine("完成"); Console.ReadLine(); } } public static void Task1Cancel(CancellationToken ct) { ct.ThrowIfCancellationRequested(); var sw = Stopwatch.StartNew(); Thread.Sleep(1000); Console.WriteLine("运行task1"); Console.WriteLine("task1:" + sw.Elapsed.ToString()); ct.ThrowIfCancellationRequested(); } public static void Task2Cancel(CancellationToken ct) { ct.ThrowIfCancellationRequested(); var sw = Stopwatch.StartNew(); Thread.Sleep(2000); Console.WriteLine("运行task2"); Console.WriteLine("task2:" + sw.Elapsed.ToString()); }
public static void RunGetStartTaskResult() { var t1 = Task.Factory.StartNew(() => GetTask1("ChengTian")); //等待t1 完成 t1.Wait(); var t2 = Task.Factory.StartNew(() => { for (int i = 0; i < t1.Result.Count; i++) { Console.WriteLine(t1.Result[i]); } }); Console.WriteLine("结束"); Console.ReadLine(); } public static List<char> GetTask1(string ss) { Thread.Sleep(1000); return ss.ToList(); }
输出返回值
public static void RunGetStartTaskContinueWith() { var t1 = Task.Factory.StartNew(() => GetTask1("ChengTian")); var t2 = t1.ContinueWith(t => { for (int i = 0; i < t1.Result.Count; i++) { Console.WriteLine(t1.Result[i]); } }); Console.WriteLine("结束"); Console.ReadLine(); }
可以在任何任务实例上调用ContinueWith方法,创建一个延续。 你也可以串联很多任务,然后等待最后一个任务(在这个实例中为t2)执行完成.
标签:status main 创建 complete 已取消 方法 命名 dispatch row
原文地址:http://www.cnblogs.com/Leo_wl/p/7198649.html