发现自己有点懒了!也可能是越往后越难了,看书理解起来有点费劲,所以这两天就每天更新一点学习笔记吧。

4.5 将APM模式转化为任务

书上提供的三种方式

方式一:

  class Program
        {
                //定义一个委托
                private delegate string AsynchronousTask(string threadName);                 static void Main(string[] args)
                {
                        //实例化一个委托对象,绑定Test函数
                        AsynchronousTask d = Test;                       Console.WriteLine("Option 1");
                      //调用TaskFactory<TResult> Factory.FromAsync()方法,这个方法有很多重载函数
                      //这个方法是 public Task<TResult> FromAsync(IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod);
                      Task<string> task = Task<string>.Factory.FromAsync(
                      d.BeginInvoke("AsyncTaskThread", Callback, "a delegate asynchronous call"), d.EndInvoke);
                      //绑定任务执行完的后续操作
                      task.ContinueWith(t => Console.WriteLine("Callback is finished, now running a continuation! Result: {0}",
                                t.Result));                         //循环打印状态信息
                        while (!task.IsCompleted)
                        {
                                Console.WriteLine(task.Status);
                                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                        }
                        Console.WriteLine(task.Status);
                        Thread.Sleep(TimeSpan.FromSeconds());                         Console.WriteLine("----------------------------------------------");
                        Console.WriteLine();
                }                 //定义一个回调函数
                private static void Callback(IAsyncResult ar)
                {
                        Console.WriteLine("Starting a callback...");
                        Console.WriteLine("State passed to a callbak: {0}", ar.AsyncState);
                        Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
                        Console.WriteLine("Thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
                }                 //定义一个委托函数
                private static string Test(string threadName)
                {
                        Console.WriteLine("Starting...");
                        Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
                        Thread.Sleep(TimeSpan.FromSeconds());
                        Thread.CurrentThread.Name = threadName;
                        return string.Format("Thread name: {0}", Thread.CurrentThread.Name);
                }

方式二:

与方式一差不多,但是使用了TaskFactory<TResult> Factory.FromAsync()方法的另一种重载,该重载并不允许指定一个将会在异步委托调用后被调用的回调函数。但是可以使用后续操作替代它。如果回调函数非常重要,建议使用第一种。

  class Program
        {
                //定义一个委托
                private delegate string AsynchronousTask(string threadName);                 static void Main(string[] args)
                {
                        //实例化一个委托对象,绑定Test函数
                        AsynchronousTask d = Test;                        Console.WriteLine("Option 2");
                       //调用TaskFactory<TResult> Factory.FromAsync()方法,这个方法有很多重载函数
                       /*
14 * 这个方法重载是
15 * public Task<TResult> FromAsync<TArg1>(Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod,
16 * Func<IAsyncResult, TResult> endMethod,
17 * TArg1 arg1,
18 * object state);
19 */
                      Task<string> task= Task<string>.Factory.FromAsync(d.BeginInvoke,d.EndInvoke,
                                                    "AsyncTaskThread",
                                             "a delegate asynchronous call");
                                
                        //绑定任务执行完的后续操作
                      task.ContinueWith(t => Console.WriteLine("Task is completed, now running a continuation! Result: {0}",
                              t.Result));                         //循环打印状态信息
                        while (!task.IsCompleted)
                        {
                                Console.WriteLine(task.Status);
                                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                        }
                        Console.WriteLine(task.Status);
                        Thread.Sleep(TimeSpan.FromSeconds());                         Console.WriteLine("----------------------------------------------");
                        Console.WriteLine();
                }                 //定义一个委托函数
                private static string Test(string threadName)
                {
                        Console.WriteLine("Starting...");
                        Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
                        Thread.Sleep(TimeSpan.FromSeconds());
                        Thread.CurrentThread.Name = threadName;
                        return string.Format("Thread name: {0}", Thread.CurrentThread.Name);
                }

方式三:

  class Program
        {
                private delegate string IncompatibleAsynchronousTask(out int threadId);                 static void Main(string[] args)
                {
                        int threadId;
                        IncompatibleAsynchronousTask e = Test;                         Console.WriteLine("Option 3");
                        
                        IAsyncResult ar = e.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");                         /*这是一个小技巧,EndMethod使用了out参数,与FromAsync的方法重载并不兼容。
15 * 然而,可以很轻松地将EndMethod调用封装到一个lambda表达式当中,从而适合
16 * 工厂方法。
17 */
                        Task<string> task = Task<string>.Factory.FromAsync(ar, _ => e.EndInvoke(out threadId, ar));
                        task.ContinueWith(t =>
                                Console.WriteLine("Task is completed, now running a continuation! Result: {0}, ThreadId: {1}",
                                        t.Result, threadId));                         while (!task.IsCompleted)
                        {
                                Console.WriteLine(task.Status);
                                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                        }
                        Console.WriteLine(task.Status);                         Thread.Sleep(TimeSpan.FromSeconds());
                }                 private static void Callback(IAsyncResult ar)
                {
                        Console.WriteLine("Starting a callback...");
                        Console.WriteLine("State passed to a callbak: {0}", ar.AsyncState);
                        Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
                        Console.WriteLine("Thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
                }                 private static string Test(out int threadId)
                {
                        Console.WriteLine("Starting...");
                        Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
                        Thread.Sleep(TimeSpan.FromSeconds());
                        threadId = Thread.CurrentThread.ManagedThreadId;
                        return string.Format("Thread pool worker thread id was: {0}", threadId);
                }
 

总结:感觉这个在日常工作当中使用的真的不是很多,比较晦涩难懂,暂且记住有TaskFactory<TResult> Factory.FromAsync()这个方法,通过这个方法可以将APM转化成TPL

4.6 将EAP模式转换成任务

例子先上:

 class Program
    {
        static void Main(string[] args)
        {
            //实例化一个TaskCompletionSource<TResult>,它是实现EAP转化成TPL的关键
            var tcs = new TaskCompletionSource<int>();             var worker = new BackgroundWorker();
            worker.DoWork += (sender, eventArgs) =>
            {
                eventArgs.Result = TaskMethod("Background worker", );
            };             worker.RunWorkerCompleted += (sender, eventArgs) =>
            {
                 //如果有错就抛出异常
                if (eventArgs.Error != null)
                {
                    tcs.SetException(eventArgs.Error);
                }
                 //如果是取消操作,就取消操作
                else if (eventArgs.Cancelled)
                {
                    tcs.SetCanceled();
                }
                else
                {
                    //正常情况返回结果
                    tcs.SetResult((int)eventArgs.Result);
                }
            };             //运行任务
            worker.RunWorkerAsync();             //获取结果
            int result = tcs.Task.Result;             Console.WriteLine("Result is: {0}", result);
        }         static int TaskMethod(string name, int seconds)
        {
            Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
                name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
            Thread.Sleep(TimeSpan.FromSeconds(seconds));
            return * seconds;
        }

4.7 实现取消选项

我们在前面说过线程工作的取消需要依靠两个类来实现,分别是CancellationTokenSource和CancellationToken这两个类

     class Program
    {
        private static void Main(string[] args)
        {
              //定义一个CancellationTokenSource类
             var cts = new CancellationTokenSource();
              //创建第一个任务,这里有个很奇怪的第地方,cts.Token被传了两次
              //分别传给了TaskMethod方法个Task的构造函数,为什么这么做呢?
             var longTask = new Task<int>(() => TaskMethod("Task 1", , cts.Token), cts.Token);
            //打印任务状态
            Console.WriteLine(longTask.Status);
            //取消任务
            cts.Cancel();
            //再次打印任务状态
            Console.WriteLine(longTask.Status);
            Console.WriteLine("First task has been cancelled before execution");             //创建第二个任务
            cts = new CancellationTokenSource();
            longTask = new Task<int>(() => TaskMethod("Task 2", , cts.Token), cts.Token);
            //启动任务
            longTask.Start();
            for (int i = ; i < ; i++ )
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                Console.WriteLine(longTask.Status);
            }
            //取消任务
            cts.Cancel();
            //打印任务状态
            for (int i = ; i < ; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                Console.WriteLine(longTask.Status);
            }             Console.WriteLine("A task has been completed with result {0}.", longTask.Result);
        }         private static int TaskMethod(string name, int seconds, CancellationToken token)
        {
            Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
                name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
            for (int i = ; i < seconds; i ++)
            {
                Thread.Sleep(TimeSpan.FromSeconds());
                //如果任务被取消,就返回-1
                if (token.IsCancellationRequested) return -;
            }
            return *seconds;
     }
}

cts.Token被传了两次为什么呢?如果在任务实际启动前取消它,该任务的TPL基础设施有责任处理该取消操作,因为这些代码根本不会被执行,通过得到第一个任务的状态可以知道它被取消了。如果尝试对该任务调用Start方法,将会得到InvalidOperationException异常。

解释:

如果在Task构造函数当中取消了,cts.Token这个参数,那么在Cts.Cancel()后面执行longTask.Start(); 会出现什么情况呢?

如下图所示,任务只有在运行操作的时候才能检查到取消操作。所以才会有WaitingToRun这个状态出现。

如果添加了这个参数,结果如下:

这个时候,在取消操作执行完后,执行开始操作就会抛出异常。

C#当中的多线程_任务并行库(中)的更多相关文章

  1. C#当中的多线程_任务并行库(上)

    复习: 第三章内容中我们提到了三种异步编程模型,这里简单复习一下,分别如下 1.APM(异步编程模式):形如Beginxxx,Endxxx. 2.EAP(基于事件的异步编程模式):这个我们在.net中 ...

  2. C#当中的多线程_任务并行库(下)

    4.8 处理任务中的异常 下面这个例子讨论了任务当中抛出异常,以及任务异常的获取     class Program     {         static void Main(string[] a ...

  3. C#多线程开发-任务并行库04

    你好,我是阿辉. 之前学习了线程池,知道了它有很多好处. 使用线程池可以使我们在减少并行度花销时节省操作系统资源.可认为线程池是一个抽象层,其向程序员隐藏了使用线程的细节,使我们可以专心处理程序逻辑, ...

  4. C#当中的多线程_线程池

    3.1 简介 线程池主要用在需要大量短暂的开销大的资源的情形.我们预先分配一些资源在线程池当中,当我们需要使用的时候,直接从池中取出,代替了重新创建,不用时候就送回到池当中. .NET当中的线程池是受 ...

  5. C#当中的多线程_线程同步

    第2章 线程同步 原来以为线程同步就是lock,monitor等呢,看了第二章真是大开眼界啊! 第一章中我们遇到了一个叫做竞争条件的问题.引起的原因是没有进行正确的线程同步.当一个线程在执行操作时候, ...

  6. C#当中的多线程_线程基础

    前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...

  7. Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介

    Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...

  8. C#多线程编程系列(五)- 使用任务并行库

    目录 1.1 简介 1.2 创建任务 1.3 使用任务执行基本的操作 1.4 组合任务 1.5 将APM模式转换为任务 1.6 将EAP模式转换为任务 1.7 实现取消选项 1.8 处理任务中的异常 ...

  9. C#并行库(TaskParallelLibrary)用法小结

    今天有空,总结一下.NET 4.5并行库(TaskParallelLibrary)用法. 也许C和C++的程序员刚刚开始写C#还习惯于new Thread来新建一个线程,但新建线程需要内存和CPU上下 ...

随机推荐

  1. x64 stack walking、调用约定、函数参数识别

    k = <rsp> <rip> <frame_count>x64下manual stack walking与x86不同,x86一般情况下有ebp chain,x64 ...

  2. org.hibernate.LazyInitializationException: failed to lazily initialize

    今天搞了一上午,都在解决这个问题:org.hibernate.LazyInitializationException: failed to lazily initialize 原因很简单,是在非法的s ...

  3. 拷贝出你GAC里面的DLL文件

    转:http://blog.sina.com.cn/s/blog_573be9bc0100ht65.html 早上和同事探讨如何从GAC中拷贝已经部署的DLL文件的时候,除了以前熟悉的命令行拷贝方法, ...

  4. HDOJ --- 2196 Computer

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. 基于.NET平台的分层架构实战(六)——依赖注入机制及IoC的设计与实现[转]

    原文:http://www.cnblogs.com/leoo2sk/archive/2008/06/19/1225223.html 我们设计的分层架构,层与层之间应该是松散耦合的.因为是单向单一调用, ...

  6. vss搭建于操作

    1.下载的vvs2005,下载后先安装在服务器上,反正就是下一步下一步就对了 安装完成后,打开miscrosoft visual sourcesafe,---create  connection da ...

  7. 在Ubuntu 中安装eclipse, eclipse 文件已经下载好!

    If you've downloaded Eclipse from their official website, follow these steps for the installation. E ...

  8. hibernate.Session简介

    ★→→SessionFactory (org.hibernate.SessionFactory) 包含已经编译的映射(mappings),是制造session的工厂,可能含有一些可以在各个事务(tra ...

  9. 浙大PTA - - 堆中的路径

    题目链接:https://pta.patest.cn/pta/test/1342/exam/4/question/21731 本题即考察最小堆的基本操作: #include "iostrea ...

  10. php 环境变量收集

    常用的PHP环境变量和系统常量列表 是一个包含服务器端相关信息的PHP全局环境变量,在PHP4.1.0之前的版本使用$HTTP_SERVER_VARS.更多信息可以参考这里. $_SERVER['PH ...