When we want to do a work asynchronously, creating a new thread is a good way. .NET provides two others ways rather than create thread explicitly, that is ThreadPool and Task.

ThreadPool

Every single CLR maintains a threadpool.There is a queue for user action request inside the threadpool.Everytime an action entry is queued by calling the ThreadPool.QueueUserWorkItem method, threadpool will pick up or create a thread to do the work.When the work is done,the thread will be returned to threadpool rather than be destroyed.

Task

ThreadPool is deficient that you can hardly know when is the work done and hardly get the returning result. In this case, Task may be a better answear.

  1. You can do a work asynchronously by calling Task.Run() method or the start() method of a instance of Task.
  2. Invoke the Wait() method to block the caller thread and wait until the task is done, meanwhile, access to the Result property of a task instance will block the caller thread, because it will call wait() method inside the Result property.
  3. Task supports cancellation just as ThreadPool do, by using the CancellationTokenSource.
  4. Can arrange one or more continuous works to be executed as soon as the task is done, by invoking the ContinueWith() method.But note that the continuous works may not be executed in the same thread of the origin work.

async and await

When access to the Result property to get the returning result of a task, the caller thread will be blocked to wait for the task get done. In this case, you can use the "await" keyword to let the control return to the caller thread while waitting for the result of the task.

If await is used inside a method, then the async modifier is required on the method, and, if a method is decorated by async, the return type of this method should only be void or Task or Task<TResult> .

Thread in depth 2:Asynchronization and Task的更多相关文章

  1. Thread in depth 4:Synchronous primitives

    There are some synchronization primitives in .NET used to achieve thread synchronization Monitor c# ...

  2. Thread in depth 3:Synchronization

    Synchronization means multi threads access the same resource (data, variable ,etc) should not cause ...

  3. Thread in depth 1: The basic

    Every single thread has the follow elements: Execution Context:Every thread has a execution context ...

  4. Task 使用 Task以及Task.Factory都是在.Net 4引用的。Task跟Thread很类似,通过下面例子可以看到。

    static public void ThreadMain() { Thread t1 = new Thread(TaskWorker); t1.Start(3); } static public v ...

  5. Task与Thread间的区别

    通过查找一些文章,得知,Task与Thread不可比.Task是为了利用多CPU多核的机制而将一个大任务不断分解成小任务,这些任务具体由哪一个线程或当前线程执行由OS来决定.如果你想自己控制由哪一个T ...

  6. C#中假设正确使用线程Task类和Thread类

    C#中使用线程Task类和Thread类小结 刚接触C#3个月左右.原先一直使用C++开发.由于公司的须要,所地採用C#开发.主要是控制设备的实时性操作,此为背景. 对于C#中的Task和Thread ...

  7. C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!

    说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...

  8. Thread.Join 和 Task.Wait 方法

    这两个方法 可以说是类似的功能,都是对当前任务进行等待阻塞,执行完毕后再进行后续处理 talk is cheap, show you code,下面一个是异步执行,一个是加了阻塞,可以对比不同执行结果 ...

  9. 那些年我们一起追逐的多线程(Thread、ThreadPool、委托异步调用、Task/TaskFactory、Parallerl、async和await)

    一. 背景 在刚接触开发的头几年里,说实话,根本不考虑多线程的这个问题,貌似那时候脑子里也有没有多线程的这个概念,所有的业务都是一个线程来处理,不考虑性能问题,当然也没有考虑多线程操作一条记录存在的并 ...

随机推荐

  1. Loadrunner通过吞吐量计算每个用户需要的带宽

    Loadrunner通过吞吐量计算每个用户需要的带宽 运行一个场景,点击Analysis进行分析,使用分析报告中的Average Throughput(bytes/second)进行计算. 计算公式: ...

  2. 常用的SQL语句(牢记)

    上课时的重要内容,其中表 t_hq, t_hq2, 以及字段的名字是举例说明. update t_hq t set t.bumendh = '10086';commit;全表更新电话,commit是提 ...

  3. HashCode的理解

    一.hashcode是什么 要理解hashcode首先要理解hash表这个概念 1. 哈希表 hash表也称散列表(Hash table),是根据关键码值(Key value)而直接进行访问的数据结构 ...

  4. python3 回顾笔记1

    http://www.runoob.com/python3/python3-tutorial.html这个网址,可以学习python3的基础语法. 1. 单引号和双引号意义完全相同.用r可以限制转义符 ...

  5. Windows phone 自定义控件(无外观控件)——ColorPicker

    编码前 在上一篇博客中,写的是一个UserControl的子类,它具有固定的外观(虽然也可以通过样式来进行修改,但受到的限制很大).如果你想要使用这个控件的逻辑,但是希望在使用的时候可以更改控件的外观 ...

  6. 139. Word Break (String; DP)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  7. 早上突然看明白 shader和材质球的关系

    计算机的世界不外乎 指令+数据 shader即Gpu指令,材质即数据

  8. mysql基本的修改表的命令

    修改表相关的命令 1.添加列表 alter table 表名 add 列名 类型; 2.删除某一列 alter table userinfo drop column 列名; 3.修改列的类型 alte ...

  9. Excel单元格内容拆分、合并

    例:如何将EXCEL单元格A1中的“1-2-1”,在B1.C1.D1单元格中分别显示”1“.”2“.”1“.方法一: 在B1中输入“=mid(A1,1,1)”在C1中输入“=mid(AI,3,1)”在 ...

  10. 设置网站默认用IE7打开

    head中加入以下内容 <meta http-equiv="X-UA-Compatible" content="IE=7" />