




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
首先,我们分析一下异步处理的环境需要在当前线程中获取返回值不需要在当前线程中获取返回值,但是仍然需要对返回值做处理对于第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 FirstWF08 09 static class Program10 11 / 12 / The main entry point for the application.13 / 14 STAThread15 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 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 Thread.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()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 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的返回值01 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();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 不需要在当前线程中获取返回值,但是仍然需要对返回值做处理 29191132001 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 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);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 大字项目库管理办法
- 厨房保洁柜管理办法
- 基建投融资管理办法
- 平安网格员管理办法
- 村集体车辆管理办法
- 残保金征缴管理办法
- 处方管理办法贵重药
- 北京残疾车管理办法
- 基建账资金管理办法
- 包头小饭桌管理办法
- 变电站钥匙管理规定
- 人教部编版小学四年级上册语文看拼音写词语及答案
- 超高效液相色谱-三重四极杆质谱联用仪技术参数
- 2024年不动产权变更协议书样本
- 干眼症的中医治疗
- 校园安全管理策略
- 四川省南充市第五中学校 2024-2025学年七年级上学期第一次诊断考试数学试题(无答案)
- 2024注册安全工程师《安全生产管理》考前十页纸
- 急性呼吸窘迫综合征
- 初中语文九级上册第三单元大单元整体教学设计 人教版
- 重庆市纤维增强改性发泡水泥保温装饰板外墙外保温系统应用技术规程
评论
0/150
提交评论