[转]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 ...
随机推荐
- hdu 5927 Auxiliary Set 贪心
Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pr ...
- asp.netMVC中,视图层和控制器层的传值
Asp.Net Mvc 控制器与视图的数据传递 摘要:本文将讨论asp.net mvc框架中的数据传递. 数据传递也就是控制器和视图之间的交互,比如在视图中提交的数据,在控制器怎么获取,或者控制器从业 ...
- [转载] LinkedIn架构这十年
原文: http://colobu.com/2015/07/24/brief-history-scaling-linkedin/ 原文: A Brief History of Scaling Link ...
- hdu3124Arbiter(最小圆距离-扫描线)
链接 详解http://blog.sina.com.cn/s/blog_6e7b12310100qnex.html #include <iostream> #include<cstd ...
- Python不同电脑之间传输文件实现类似scp功能不输密码
SCP vs SFTP 通过paramiko还可以传输文件,如何通过paramiko在计算机之间传输文件,通过阅读官方文档,发现有如下两种方式: sftp = paramiko.SFTPClient. ...
- Illegal mix of collations (big5_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation 'like'
解释: 非法的混合排序规则(big5_chinese_ci)和(utf8_general_ci)操作“like”. 原本是单个字段查询数据的,现在是把所有的字段用一个搜索框来查询. 主要出问题是下列这 ...
- 20160805_Cent6.4x64_安装配置(含网卡驱动的配置)
ZC: 全程 root用户 操作. 1.我在BIOS中将 UEFI关闭了,然后 才安装的 Cent6.4x64 (ZC: 安装系统时,一起安装了 gcc等一些编程用的包.本来是想安装QT时少点麻烦的, ...
- hiho_1089_floyd最短路
题目 floyd算法求所有顶点之间的最短路,典型的模板题.唯一需要注意的是两个顶点之间可能有多条边直接相连,在初始化的时候,直接选择最小的长度作为两点间的距离即可. 实现 #include<io ...
- python网络编程socket之多线程
#coding:utf-8 __author__ = 'similarface' import os,socket,threading,SocketServer SERVER_HOST='localh ...
- opencv249配置
vs环境变量 opencv环境变量 D:\opencv\opencv\build\x86\vc12\bin D:\opencv\opencv\build\x64\vc12\bin 包含目录 D:\op ...