软件构架师总结出的经验-c#异步调用的几种方式-经典啊.doc

上传人:枫** 文档编号:544577077 上传时间:2023-08-05 格式:DOC 页数:12 大小:36KB
返回 下载 相关 举报
软件构架师总结出的经验-c#异步调用的几种方式-经典啊.doc_第1页
第1页 / 共12页
软件构架师总结出的经验-c#异步调用的几种方式-经典啊.doc_第2页
第2页 / 共12页
软件构架师总结出的经验-c#异步调用的几种方式-经典啊.doc_第3页
第3页 / 共12页
软件构架师总结出的经验-c#异步调用的几种方式-经典啊.doc_第4页
第4页 / 共12页
软件构架师总结出的经验-c#异步调用的几种方式-经典啊.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《软件构架师总结出的经验-c#异步调用的几种方式-经典啊.doc》由会员分享,可在线阅读,更多相关《软件构架师总结出的经验-c#异步调用的几种方式-经典啊.doc(12页珍藏版)》请在金锄头文库上搜索。

1、软件构架师总结出的经验-c#异步调用的几种方式-经典啊首先,我们分析一下异步处理的环境需要在当前线程中获取返回值不需要在当前线程中获取返回值,但是仍然需要对返回值做处理对于第1中情况,还可以继续细分在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前线程中获取T的返回值在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值下面,我将一一给出例子:1.1 在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前

2、线程中获取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 FirstWF08 09 static class Program10 11 / 12 / The main entry point for the application.13 / 14 STAThread1

3、5 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.WriteLi

4、ne(Implement other tasks end .);23 Console.WriteLine(Get users 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 T

5、hread.Sleep(5000);33 Console.WriteLine(Func end to run);34 return userInput.ToString();35 36 37 输出结果如下:Implement other tasksFunc start to run.Func end to runImplement other tasks end .Get users input561.2 在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值01 static void Main()0

6、2 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.Clos

7、e();09 Console.WriteLine(Implment task 2);10 Console.WriteLine(Get users input);11 Console.WriteLine(caller.EndInvoke(result);12 Console.ReadLine();13 输出结果如下:Input number please.25Implement task 1Func start to run.Func end to runImplment task 2Get users input251.3 在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值0

8、1 STAThread02 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(

9、);11 12 Console.WriteLine();13 Console.WriteLine(Implement other task2);14 Console.WriteLine(Get users input);15 Console.WriteLine(caller.EndInvoke(result);16 Console.ReadLine();17 输出结果如下:Func start to run.Func end to runImplement other task2Get users input232 不需要在当前线程中获取返回值,但是仍然需要对返回值做处理 2919113200

10、1 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 FirstWF08 09 static class Program10 11 / 12 / The main entry point for the application.13 / 14 STAThread15 static vo

11、id 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);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号