c#异步调用的几种方式

发表于:2010-1-18 10:12

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

#
DotNet

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

  需要在当前线程中获取返回值

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

  对于第1中情况,还可以继续细分

  在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前线程中获取T的返回值

  在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值

  在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值

  下面,我将一一给出例子:

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

  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的返回值

  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

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号