[转]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 ...
随机推荐
- openstack 网卡
桥接基本原理: 物理网卡eth0 br0(桥) tap0,tap1(tap是给vm使用的接口)
- iOS - OC NSProcessInfo 系统进程信息
前言 @interface NSProcessInfo : NSObject NSProcessInfo 类中包含一些方法,允许你设置或检索正在运行的应用程序(即进程)的各种类型的信息. 1.获取系统 ...
- 07 SQL优化技术
本章提要------------------------------------------------------调优技术及什么时候使用------------------------------- ...
- otl插入数据不成功
原因是:void rlogon(...); 没有设置auto_commit为1,otl不会自动提交. 注意:static int otl_initialize (const int threaded_ ...
- Oracle 10g实现存储过程异步调用
DBMS_JOB是什么?DBMS_JOB是Oracle数据库提供的专家程序包的一个.主要用来在后台运行程序,是数据库中一个极好的工具. 可用于自动调整调度例程任务,例如分析数据表,执行一些归档操作,清 ...
- Android 四种简单的动画(淡入淡出、旋转、移动、缩放效果)
最近在Android开发当中,用到的动画效果. public void onClick(View arg0) { // TODO 自动生成的方法存根 switch (arg0.getId()) { c ...
- widnow.open
http://blog.csdn.net/chenyanggo/article/details/7443051
- 同一个服务器部署多个tomcat
下面我们把配置的详细过程写在下面,以供参考:(此例以配置三个Tomcat为例) 1. 下载apache-tomcat-7.0.63,下载下来的文件为apache-tomcat-7.0.63.zip. ...
- eclipse 下的 merge 是如何实现的
1 我从eclipse里面新建一个workspace2 新建一个分支3 再新建另外一个分支4 回到原分支, 修改某一行,比如a.txt的85行5 提交git add commit6 切换到新分支7 修 ...
- Open-falon监控安装过程
Open-falon监控安装过程 具体参考: http://book.open-falcon.org/zh/quick_install/prepare.html 1. 安装ntp.vim编辑器 ...