异步操作的本质

  在方法调用前为异步方法指定一个回调函数,方法调用后被线程池中的一个线程接管,执行该方法。主线程立即返回,继续执行其他工作或响应用户请求。如果异步方法执行完    毕,回调函数被自动执行,以处理异步方法的调用结果。 如何实现异步方法呢?C#通过异步委托调用BeginInvoke和EndInvoke方法来实现异步方法。

  BeginInvoke方法原型: IAsyncResult BeginInvoke(......, AsyncCallback callback, object o); ......表示异步委托中定义的参数列表。 AsyncCallback参数是一个用于  回调函数的委托,它的原型为: public delegate void AsyncCallback(IAsyncResult ar)。其中IAsyncResult参数用于包装异步方法的执行结果。 Object参数用于在主线程与回调函数间传递一些附加信息,如同步信息。

EndInvoke方法原型: xxx EndInvoke(IAsyncResult result); xxx表示异步委托原型中定义的返回数据类型,IAsyncResult用于包装异步方法的执行结果。

  线程的本质
  线程不是一个计算机硬件的功能,而是操作系统提供的一种逻辑功能,线程本质上是进程中一段并发运行的代码,所以线程需要操作系统投入CPU资源来运行和调度。

  异步操作的优缺点

  因为异步操作无须额外的线程负担,并且使用回调的方式进行处理,在设计良好的情况下,处理函数可以不必使用共享变量(即使无法完全不用,最起码可以减少共享变量的数量),减少了死锁的可能。当然异步操作也并非完美无暇。编写异步操作的复杂程度较高,程序主要使用回调方式进行处理,与普通人的思维方式有些初入,而且难以调试。

  多线程的优缺点
  多线程的优点很明显,线程中的处理程序依然是顺序执行,符合普通人的思维习惯,所以编程简单。但是多线程的缺点也同样明显,线程的使用(滥用)会给系统带来上下文切换的额外负担。并且线程间的共享变量可能造成死锁的出现。

  适用范围

  在了解了线程与异步操作各自的优缺点之后,我们可以来探讨一下线程和异步的合理用途。我认为:当需要执行I/O操作时,使用异步操作比使用线程+同步I/O操作更合适。I/O操作不仅包括了直接的文件、网络的读写,还包括数据库操作、Web Service、HttpRequest以及.Net Remoting等跨进程的调用。
  而线程的适用范围则是那种需要长时间CPU运算的场合,例如耗时较长的图形处理和算法执行。但是往往由于使用线程编程的简单和符合习惯,所以很多朋友往往会使用线程来执行耗时较长的I/O操作。这样在只有少数几个并发操作的时候还无伤大雅,如果需要处理大量的并发操作时就不合适了。

下面看个异步调用的实例:

using System;
using System.Threading; namespace AsyncDelegateDemo
{
delegate void AsyncFoo(int i);
class Program
{
///<summary>
/// 输出当前线程的信息
///</summary>
///<param name="name">方法名称</param> static void PrintCurrThreadInfo(string name)
{
Console.WriteLine("Thread Id of " + name+ " is: " + Thread.CurrentThread.ManagedThreadId+ ", current thread is "
+ (Thread.CurrentThread.IsThreadPoolThread ? "" : "not ")
+ "thread pool thread.");
} ///<summary>
/// 测试方法,Sleep一定时间
///</summary>
///<param name="i">Sleep的时间</param>
static void Foo(int i)
{
PrintCurrThreadInfo("Foo()");
Thread.Sleep(i);
} ///<summary>
/// 投递一个异步调用
///</summary>
static void PostAsync()
{
AsyncFoo caller = new AsyncFoo(Foo);
caller.BeginInvoke(, new AsyncCallback(FooCallBack), caller);
} static void Main(string[] args)
{
PrintCurrThreadInfo("Main()");
for(int i = ; i < ; i++)
{
PostAsync();
}
Console.ReadLine();
} static void FooCallBack(IAsyncResult ar)
{
PrintCurrThreadInfo("FooCallBack()");
AsyncFoo caller = (AsyncFoo) ar.AsyncState;
caller.EndInvoke(ar); }
}
}

这段代码代码的输出如下:

Thread Id of Main() is: , current thread is not thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of Foo() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.
Thread Id of FooCallBack() is: , current thread is thread pool thread.

c# 多线程与异步调用的更多相关文章

  1. 异步和多线程,委托异步调用,Thread,ThreadPool,Task,Parallel,CancellationTokenSource

    1 进程-线程-多线程,同步和异步2 异步使用和回调3 异步参数4 异步等待5 异步返回值 5 多线程的特点:不卡主线程.速度快.无序性7 thread:线程等待,回调,前台线程/后台线程, 8 th ...

  2. Java多线程实现异步调用

    在Java平台,实现异步调用的角色有如下三个角色:调用者. 提货单 .真实数据,一个调用者在调用耗时操作,不能立即返回数据时,先返回一个提货单 .然后在过一断时间后凭提货单来获取真正的数据.去蛋糕店买 ...

  3. 衔接UI线程和管理后台工作线程的类(多线程、异步调用)

    一个不错的UI多线程操作类 http://www.cnblogs.com/net66/archive/2005/08/03/206132.html

  4. 【摘要】多线程 - BeginInvoke异步调用

    private delegate int MyMethod(); private int method() { Thread.Sleep(); ; } private void MethodCompl ...

  5. C# 多线程详解 Part.02(UI 线程和子线程的互动、ProgressBar 的异步调用)

           我们先来看一段运行时会抛出 InvalidOperationException 异常的代码段: private void btnThreadA_Click(object sender, ...

  6. PHP中实现异步调用多线程程序代码

    本文章详细的介绍了关于PHP中实现异步调用多线程方法,下面我们以给1000个用户发送一封推荐邮件,用户输入或者导入邮件账号了提交服务器执行发送来讲述. 比如现在有一个场景,给1000个用户发送一封推荐 ...

  7. 多线程编程学习笔记——异步调用WCF服务

    接上文 多线程编程学习笔记——使用异步IO 接上文 多线程编程学习笔记——编写一个异步的HTTP服务器和客户端 接上文 多线程编程学习笔记——异步操作数据库 本示例描述了如何创建一个WCF服务,并宿主 ...

  8. MacOS和iOS开发中异步调用与多线程的区别

    很多童鞋可能对Apple开发中的异步调用和多线程的区别不是太清楚,这里本猫将用一些简单的示例来展示一下它们到底直观上有神马不同. 首先异步调用可以在同一个线程中,也可以在多个不同的线程中.每个线程都有 ...

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

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

随机推荐

  1. journalctl --help

    journalctl [OPTIONS...] [MATCHES...] Query the journal. Flags:     --system              Show the sy ...

  2. 关于Depth Bounds Test (DBT)和在CE3的运用

    Depth Bounds Test (DBT) Depth Bounds Test(深度范围检测),是Nvdia GeForce 6系列以后显卡的特性(GPU Programming Guide Ge ...

  3. endsWith和startsWith同样效果其他形式的写法(2016.1.12)

    //判断以什么开始startWith str = "abcdef"; //用其他的形式写的startsWith if(str.indexOf("abc")==0 ...

  4. 大话数据结构(十二)java程序——KMP算法及改进的KMP算法实现

    1.朴素的模式匹配算法 朴素的模式匹配算法:就是对主串的每个字符作为子串开头,与要连接的字符串进行匹配.对主串做大循环,每个字符开头做T的长度的小循环,直到成功匹配或全部遍历完成为止. 又称BF算法 ...

  5. hot code replace

    http://wiki.eclipse.org/FAQ_What_is_hot_code_replace%3F https://social.msdn.microsoft.com/Forums/vst ...

  6. ubuntu 工作区中拖动一个窗体到另一个工作区就卡住回不到桌面了

    ubuntu 工作区中拖动一个窗体到另一个工作区就卡住回不到桌面了 解决方法: 按 alt + 回车  键直接就返回去了

  7. JS获取Cookie值

    function GetLoginCookie() { var userCookie = getCookie("mycookie"); var loginname = userCo ...

  8. 文件对比工具Beyond Compare使用方法

    今天向大家介绍一个使用起来十分方便且功能十分强大的文件对比工具-Beyond Compare. 1    工具下载 工具的下载很简单,百度搜索Beyond Compare即可. 下载完成后,解压缩,双 ...

  9. 【Android测试】【第七节】Monkey——源码浅谈

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4713466.html 前言 根据上一篇我们学会了Monke ...

  10. json-lib 之jsonConfig具体应用

    一,setCycleDetectionStrategy 防止自包含 public static void testCycleObject() {         CycleObject object ...