[转]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 ...
随机推荐
- C# DataGridView控件绑定数据后清空数据
//1.this.dataGridView1.DataSource = null;//会将DataGridView的列也删掉 //2.this.dataGridView1.Columns.Clear( ...
- XAF应用开发教程(七)外观控制模块
很多时候,我们需要按照不同的条件显示不同的效果,在传统的软件开发中,我们会直接使用 控件名称.BackColor,Enable,Visible等属性进行控制. 如果一个业务对象在多处使用,要么我们会去 ...
- Java中枚举类型简单学习
/* * enum类型不允许继承 * 除了这一点,我们基本上可以将enum看作一个常规的类 * 我们可以添加自己的方法与属性,我们也可以覆盖其中的方法. * 不过一定要在enum实例序列的最后添加分号 ...
- Oracle中synonym和index
笔记: Oracle-同义词--通过用户名(模式名).表名 --授权:grant create synonym to test1(system用户下授权)) --私有 creat ...
- FileCopy
/*[入]指的是到内存里,[出]指的是到内存外*/ import java.io.*; public class MyReadFile{ public static void main(String[ ...
- UNC path
http://www.emailsignature.eu/phpBB2/how-to-get-a-unc-path-name-t341.html In a network, the Universal ...
- (一)S5PV210开发板常用易忘操作记录
一.调试串口 2.SD卡槽 (三)启动方式选择 蜂鸣器下面的白色2针插座(图中红色线圈出来的那个)为选择USB/SD卡启动的开关.默认情况下为SD卡启动,如果需要USB启动则使用短路帽(若没有短路帽, ...
- android平台的技术架构
Android平台采用了软件堆层(Software Stack)的架构,主要分为四个部分: 1.应用软件 Android 连同一个核心应用程序包一起发布,该应用程序包包括E-mail客户端.SMS短消 ...
- hdu 1588(Fibonacci矩阵求和)
题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...
- java实现数据库连接池
package com.kyo.connection; import java.sql.Connection; import java.sql.DatabaseMetaData; import jav ...