首先,我们分析一下异步处理的环境

需要在当前线程中获取返回值
不需要在当前线程中获取返回值,但是仍然需要对返回值做处理
对于第1中情况,还可以继续细分

在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前线程中获取T的返回值
在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值
在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值
下面,我将一一给出例子:

1.1 在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前线程中获取T的返回值
view sourceprint?01 using System;

02 using System.Collections.Generic;

03 using System.Linq;

04 using System.Windows.Forms;

05 using System.Threading;

06 using System.Runtime.Remoting.Messaging;

07 namespace FirstWF

08 {

09     static class Program

10     {

11         /// <summary>

12         /// The main entry point for the application.

13         /// </summary>

14         [STAThread]

15         static void Main()

16         {

17             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

18             Console.WriteLine("Input number please...");

19             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);

20             Console.WriteLine("Implement other tasks");

21             Thread.Sleep(7000);

22             Console.WriteLine("Implement other tasks end ...");

23             Console.WriteLine("Get user's input");

24             Console.WriteLine(caller.EndInvoke(result));

25             Console.ReadLine();

26         }

27         delegate string AsyncFuncDelegate(int userInput);

28         static string Func(int userInput)

29         {

30             Console.WriteLine("Func start to run");

31             Console.WriteLine("...");

32             Thread.Sleep(5000);

33             Console.WriteLine("Func end to run");

34             return userInput.ToString();

35         }

36     }

37 }

输出结果如下:

Implement other tasks

Func start to run

...

Func end to run

Implement other tasks end ...

Get user's input

56

1.2 在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值
view sourceprint?01 static void Main()

02         {

03             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

04             Console.WriteLine("Input number please...");

05             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);

06             Console.WriteLine("Implement task 1");

07             result.AsyncWaitHandle.WaitOne();

08             result.AsyncWaitHandle.Close();

09             Console.WriteLine("Implment task 2");

10             Console.WriteLine("Get user's input");

11             Console.WriteLine(caller.EndInvoke(result));

12             Console.ReadLine();

13         }

输出结果如下:

Input number please...

25

Implement task 1

Func start to run

...

Func end to run

Implment task 2

Get user's input

25

1.3 在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值
view sourceprint?01 [STAThread]

02         static void Main()

03         {

04             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

05             Console.WriteLine("Input number please...");

06             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);

07             while (!result.IsCompleted)

08             {

09                 Thread.Sleep(1000);

10                 Console.Write(">");

11             }

12             Console.WriteLine("");

13             Console.WriteLine("Implement other task2");

14             Console.WriteLine("Get user's input");

15             Console.WriteLine(caller.EndInvoke(result));

16             Console.ReadLine();

17         }

输出结果如下:

Func start to run

...

>>>>>Func end to run

>

Implement other task2

Get user's input

23

2 不需要在当前线程中获取返回值,但是仍然需要对返回值做处理

view sourceprint?01 using System;

02 using System.Collections.Generic;

03 using System.Linq;

04 using System.Windows.Forms;

05 using System.Threading;

06 using System.Runtime.Remoting.Messaging;

07 namespace FirstWF

08 {

09     static class Program

10     {

11         /// <summary>

12         /// The main entry point for the application.

13         /// </summary>

14         [STAThread]

15         static void Main()

16         {

17             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);

18             Console.WriteLine("Input number please...");

19             caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), new AsyncCallback(CallBackFunc), "Message from Main thread.");

20             Console.WriteLine("Main thread ends");

21             Console.ReadLine();

22         }

23         delegate string AsyncFuncDelegate(int userInput);

24         static string Func(int userInput)

25         {

26             Console.WriteLine("Func start to run");

27             Console.WriteLine("...");

28             Thread.Sleep(5000);

29             Console.WriteLine("Func end to run");

30             return userInput.ToString();

31         }

32         static void CallBackFunc(IAsyncResult ar)

33         {

34             AsyncResult result = ar as AsyncResult;

35             string inputMessage = result.AsyncState as string;

36             AsyncFuncDelegate caller = result.AsyncDelegate as AsyncFuncDelegate;

37             Console.WriteLine("call back starts");

38             Console.WriteLine(inputMessage);

39             Console.WriteLine("The input number is : " + caller.EndInvoke(ar));

40             Console.WriteLine("call back ends");

41         }

42     }

43 }

输出结果如下:

Input number please...

23

Main thread ends

Func start to run

...

Func end to run

call back starts

Message from Main thread.

The input number is : 23

call back ends

c#异步调用的几种方式的更多相关文章

  1. 说说Java异步调用的几种方式

    日常开发中,会经常遇到说,前台调服务,然后触发一个比较耗时的异步服务,且不用等异步任务的处理结果就对原服务进行返回.这里就涉及的Java异步调用的一个知识.下面本文尝试将Java异步调用的多种方式进行 ...

  2. Java 异步编程的几种方式

    前言 异步编程是让程序并发运行的一种手段.它允许多个事情同时发生,当程序调用需要长时间运行的方法时,它不会阻塞当前的执行流程,程序可以继续运行,当方法执行完成时通知给主线程根据需要获取其执行结果或者失 ...

  3. DLL调用的两种方式(IDE:VC6.0,C++)

    原文:http://www.cnblogs.com/Pickuper/articles/2050409.html DLL调用有两种方式,一种是静态调用,另外一种是动态调用 (一)静态调用 静态调用是一 ...

  4. python 模块调用的几种方式

    在python里面又很多模块,或者引用第三方模块,python 模块调用的几种方式,下面详细解说 1,import 模块名 2,from 模块 import  模块里面的小功能 3,from  模块 ...

  5. 【整理】Ajax异步实现的几种方式总结

    AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术.GET ...

  6. JavaScript处理异步请求的几种方式(取异步函数返回值)

    JavaScript处理异步的几种方式 Javascript语言的执行环境是"单线程"(single thread,就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个 ...

  7. [OpenSource]浅谈.Net和Java互相调用的三种方式

    在很多的大型系统开发中,开发工具往往不限制于同一种开发语言,而是会使用多种开发语言的混合型开发.目前Java和.Net都声称自己占85%的市场份额,不管谁对谁错,Java和.Net是目前应用开发的两个 ...

  8. 浅谈.Net和Java互相调用的三种方式

    在很多的大型系统开发中,开发工具往往不限制于同一种开发语言,而是会使用多种开发语言的混合型开发.目前Java和.Net都声称自己占85%的市场份 额,不管谁对谁错,Java和.Net是目前应用开发的两 ...

  9. Struts2方法调用的三种方式

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="student" class="com.itmyho ...

随机推荐

  1. ES6-模块化

    ES6-模块化 在es6标准中,js原生支持modulele. ES6模块需要使用babel转码,这里简单解释一下什么是babel转码. babel就是将‘ES6模块化语法’转化为‘CommonJS模 ...

  2. 聊聊Java中几种常用的设计模式

    1.单例模式(有的书上说叫单态模式其实都一样) 该模式主要目的是使内存中保持1个对象.看下面的例子: package org.sp.singleton; //方法一 public class Sing ...

  3. iKcamp出品|全网最新|微信小程序|基于最新版1.0开发者工具之初中级培训教程分享

  4. 初学者易上手的SSH-struts2 04值栈与ognl表达式

    什么是值栈?struts2里面本身提供的一种存储机制,类似于域对象,值栈,可以存值和取值.,特点:先进后出.如果将它当做一个容器的话,而这个容器有两个元素,那么最上面的元素叫做栈顶元素,也就是所说的压 ...

  5. Httpd2.2常见配置及功能

    Httpd 2.2常见配置 要配置http服务的配置文件,先备份一下,养成良好习惯,如果误操作导致http服务起不来,就可以将备份的主配置文件重新覆盖一下 httpd配置文件的组成:有三大部分组成,其 ...

  6. (10.23)Java小知识!

    ---恢复内容开始--- 方法的定义: 一般情况下,定义一个方法包含以下语法: 修饰符 返回值类型 方法名 (参数类型 参数名 , ...){ ... 方法体 ... return 返回值; } 修饰 ...

  7. C#通过OpenCL调用显卡GPU做高效并行运算

    GPU的并行运算能力远超CPU,有时候我们会需要用到超大数据并行运算,可以考虑用GPU实现,这是一篇C#调用GPU进行运算的入门教程. 1: 下载相关的库: https://sourceforge.n ...

  8. JPA + SpringData 操作数据库 ---- 深入了解 SpringData

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7735616.html ------------------------------------ ...

  9. js代码执行顺序问题

      前  言 LiuDaP 今天就给大家介绍一个特别基础的东西,javascript中函数的一点儿小知识(js代码的执行顺序),希望对大家有那么一点点帮助吧!!! 一.js--->单线程 严格意 ...

  10. DataTable数据修改,换列

    增加列             DataTable table= new DataTable();             table.Columns.Add("ID", type ...