[转]BeginInvoke和EndInvoke方法浅析
1. using System;
2.
3. using System.Collections.Generic;
4.
5. using System.Linq;
6.
7. using System.Text;
8.
9. using System.Threading;
10.
11. namespace MyThread
12.
13. {
14.
15. class Program
16.
17. {
18.
19. private static int newTask(int ms)
20.
21. {
22.
23. Console.WriteLine("任务开始");
24.
25. Thread.Sleep(ms);
26.
27. Random random = new Random();
28.
29. int n = random.Next(10000);
30.
31. Console.WriteLine("任务完成");
32.
33. return n;
34.
35. }
36.
37. private delegate int NewTaskDelegate(int ms);
38.
39. static void Main(string[] args)
40.
41. {
42.
43. NewTaskDelegate task = newTask;
44.
45. IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);
46.
47. // EndInvoke方法将被阻塞2秒
48.
49. int result = task.EndInvoke(asyncResult);
50.
51. Console.WriteLine(result);
52.
53. }
54.
55. }
56.
57. }
58.
1. Thread.Sleep(10000);
2.
1. static void Main(string[] args)
2.
3. {
4.
5. NewTaskDelegate task = newTask;
6.
7. IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);
8.
9. while(!asyncResult.IsCompleted)
10.
11. {
12.
13. Console.Write("*");
14.
15. Thread.Sleep(100);
16.
17. }
18.
19. // 由于异步调用已经完成,因此, EndInvoke会立刻返回结果
20.
21. int result = task.EndInvoke(asyncResult);
22.
23. Console.WriteLine(result);
24.
25. }
26.
1. static void Main(string[] args)
2.
3. {
4.
5. NewTaskDelegate task = newTask;
6.
7. IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);
8.
9. while(!asyncResult.AsyncWaitHandle.WaitOne(100, false))
10.
11. {
12.
13. Console.Write("*");
14.
15. }
16.
17. int result = task.EndInvoke(asyncResult);
18.
19. Console.WriteLine(result);
20.
21. }
22.
1. private delegate int MyMethod();
2.
3. private int method()
4.
5. {
6.
7. Thread.Sleep(10000);
8.
9. return 100;
10.
11. }
12.
13. private void MethodCompleted(IAsyncResult asyncResult)
14.
15. {
16.
17. if(asyncResult == null) return;
18.
19. textBox1.Text =(asyncResult.AsyncState as
20.
21. MyMethod).EndInvoke(asyncResult).ToString();
22.
23. }
24.
25. private void button1_Click(object sender, EventArgs e)
26.
27. {
28.
29. MyMethod my = method;
30.
31. IAsyncResult asyncResult = my.BeginInvoke(MethodCompleted, my);
32.
33. }
34.
1. private void requestCompleted(IAsyncResult asyncResult)
2.
3. {
4.
5. if(asyncResult == null) return;
6.
7. System.Net.HttpWebRequest hwr = asyncResult.AsyncState as System.Net.HttpWebRequest;
8.
9. System.Net.HttpWebResponse response =
10.
11. (System.Net.HttpWebResponse)hwr.EndGetResponse(asyncResult);
12.
13. System.IO.StreamReader sr = new
14.
15. System.IO.StreamReader(response.GetResponseStream());
16.
17. textBox1.Text = sr.ReadToEnd();
18.
19. }
20.
21. private delegate System.Net.HttpWebResponse RequestDelegate(System.Net.HttpWebRequest request);
22.
23. private void button1_Click(object sender, EventArgs e)
24.
25. {
26.
27. System.Net.HttpWebRequest request =
28.
29. (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.cnblogs.com");
30.
31. IAsyncResult asyncResult =request.BeginGetResponse(requestCompleted, request);
32.
33. }
34.
[转]BeginInvoke和EndInvoke方法浅析的更多相关文章
- BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题
BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题 原文:http://www.sufeinet.com/thread-3707-1-1.html 大家可以先看看我上 ...
- C#线程系列讲座(1):BeginInvoke和EndInvoke方法
一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能,将要执行的任务分解成多个子任务执行.这就需要在同一个进程中开启多个 ...
- delegate 中的BeginInvoke和EndInvoke方法
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能 ...
- 委托的BeginInvoke和EndInvoke方法
.NET Framework 允许异步调用任何方法,为了实现异步调用目标,需要定义与被调用方法具有相同签名的委托.公共语言运行时会自动使用适当的签名为该委托定义 BeginInvoke 和 EndIn ...
- 用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程
让我们首先了解下什么时候用到C#异步调用: .NET Framework 允许您C#异步调用任何方法.定义与您需要调用的方法具有相同签名的委托:公共语言运行库将自动为该委托定义具有适当签名的Begin ...
- 【C#】用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程
让我们首先了解下什么时候用到C#异步调用: .NET Framework 允许您C#异步调用任何方法.定义与您需要调用的方法具有相同签名的委托:公共语言运行库将自动为该委托定义具有适当签名的Begin ...
- 转:C#线程系列讲座(1) BeginInvoke和EndInvoke方法
转载自:http://www.cnblogs.com/levin9/articles/2319248.html 开发语言:C#3.0IDE:Visual Studio 2008本系列教程主要包括如下内 ...
- 黄聪:C#多线程教程(1):BeginInvoke和EndInvoke方法,解决主线程延时Thread.sleep柱塞问题(转)
开发语言:C#3.0 IDE:Visual Studio 2008 本系列教程主要包括如下内容: 1. BeginInvoke和EndInvoke方法 2. Thread类 3. 线程池 4. 线 ...
- C# BeginInvoke和EndInvoke方法
转载自:BeginInvoke和EndInvoke方法 IDE:Visual Studio 2008 本系列教程主要包括如下内容:1. BeginInvoke和EndInvoke方法 2. Threa ...
随机推荐
- 保存会话数据——session学习
Session:Session是服务器端技术,利用这个技术,服务器在运行时可以为每一个用户的浏览器创建一个其独享的session对象,由于session为用户浏览器独享,所以用户在访问服务器的web资 ...
- 用Java集合中的Collections.sort方法对list排序的两种方法
用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 文件MD5校验
1. 以前记得是在 msdn.itellyou.cn 上下载的 MD5 校验工具,应该是 IHasher,但是现在 msdn.itellyou.cn 上搜不到这个工具了... 2.
- Maven核心概念之依赖,聚合与继承
一.依赖 我们项目中依赖的jar包可以通过依赖的方式(dependencies元素下添加dependency子元素)引入. <dependency> <groupId>juni ...
- .net ftp上传文件方法
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- VIM跳到指定行
ngg nG :n n is the line number
- GO数据库
Golang 数据库操作 Golang 数据库 MySQL Golang支持DB操作位于database包下,支持基本CRUD操作.事务和Prepared Statement,本文以MySQL为例. ...
- Java中的Double类型计算
一.问题的提出: 如果我们编译运行下面这个程序会看到什么?public class Test{ public static void main(String args[]){ Sy ...
- Android_用户界面概述和数据单位
一.UI界面概述 UI,对于一个应用而言用户界面是非常重要的一部分,是应用的脸,用户对应用第一个印象来自于界面,因此如果没有完美的用户界面,很难留住用户. 好的用户界面会极大提高用户的使用欲望并维护客 ...
- xcode黑科技
1多开模拟器 使用命令行: cd /Applications/Xcode1.app/Contents/Developer/Applications/&open -n Simulator.app ...