[转]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 ...
随机推荐
- ecnu1624求交集多边形面积
链接 本来在刷hdu的一道题..一直没过,看到谈论区发现有凹的,我这种方法只能过凸多边形的相交面积.. 就找来这道题试下水. 两个凸多边形相交的部分要么没有 要么也是凸多边形,那就可以把这部分单独拿出 ...
- thinkphp ajax 无刷新分页效果的实现
思路:先做出传统分页效果,然后重新复制一份Page.class.php类,对它进行修改,把js中的函数传到page类中,把上一页.下一页.首页.尾页.链接页中的url地址改成js控制的函数,模板页面中 ...
- JQuery $()后面的括号里的内容什么时候加引号,什么时候不加
一.如果是已经声明存在的变量或者对象,就不用加引号. 比如var name=document.getElementById("name"); $(name)或者$(this). 二 ...
- OpenGL的glOrtho平行投影函数详解[转]
glortho函数可以将当前的可视空间设置为正投影空间.基参数的意义如图,如果绘制的图空间本身就是二维的,可以使gluOrtho2D.他的使用类似于glOrtho 原型是: void glOrtho( ...
- [转]Oracle中INITRANS和MAXTRANS参数
每个块都有一个块首部.这个块首部中有一个事务表.事务表中会建立一些条目来描述哪些事务将块上的哪些行/元素锁定.这个事务表的初始大小由对象的INITRANS 设置指定.对于表,这个值默认为2(索引的IN ...
- java 嵌套类 简记
嵌套类包括:1)静态嵌套类 (static 修饰符) 2)非静态嵌套类(又叫内部类) 其中内部类又可分为三种: 其一.在一个类(外部类)中直接定义的内部类: 其二.在一个方法(外部类的方法)中定义的 ...
- K均值聚类(Kmeans)
Sigma = [1, 0; 0, 1]; mu1 = [1, -1]; x1 = mvnrnd(mu1, Sigma, 200); mu2 = [5.5, -4.5]; x2 = mvnrnd(mu ...
- 无法找到脚本*.VBS的脚本引擎解决办法
当你在运行一些基于VBS脚本语言的文件时,系统可能报错.这时候可能是你的VBS脚本服务在注册表中出错了,原因可能是卸载或安装一些代码不规范的程序引起的.这里给出无法找到脚本引擎"vbscri ...
- Unity3D研究院编辑器之不实例化Prefab获取删除更新组件(十五)
http://www.xuanyusong.com/archives/3727 感谢楼下的牛逼回复更正一下,我表示我也是才知道.. 其实不需要实例化也能查找,你依然直接用GetComponentsIn ...
- C语言中随机数相关问题
用C语言产生随机数重要用到rand函数.srand函数.及宏RAND_MAX(32767),它们均在stdlib.h中进行了声明. int rand(void);//生成一个随机数 voidsrand ...