AsyncCallback IAsyncResult
using System;
using System.Threading;
using System.Collections.Generic;
using System.Windows.Forms; namespace AsyncDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} class objstate //申明一个实体类
{
public string fname;
public string lname;
public DateTime birthday;
public objstate()
{
fname = "wang";
lname = "nima";
birthday = DateTime.Now;
}
public objstate(string fn, string ln)
{
fname = fn;
lname = ln;
//birthday = DateTime.Now;
}
} AsyncCallback callback;
objstate obj; #region easy demo
//申明委托
public delegate string deltest();
deltest begin;
private void button1_Click(object sender, EventArgs e)
{
begin = new deltest(method);
callback = new AsyncCallback(back);
obj = new objstate();//实例化类,该对象可以传入回调函数中
begin.BeginInvoke(back, obj);//异步执行method,界面不会假死,5秒后执行回调函数,弹出提示框
}
private void back(IAsyncResult ar)
{
string res = begin.EndInvoke(ar);
objstate obj = (objstate)(ar.AsyncState);//通过AsyncState获取传入的object
MessageBox.Show(res + "\n" + obj.fname + " " + obj.lname + " " + obj.birthday); } private string method()
{
Thread.Sleep(5000);
return "welcome to this world";
}
#endregion #region complex demo //申明委托
public delegate void deltest1();
deltest1 begin1;
public delegate string deltest2(List<string> list);
deltest2 begin2;
private void button2_Click(object sender, EventArgs e)
{
string id = Thread.CurrentThread.ManagedThreadId.ToString();
Thread.CurrentThread.Name = "MainThread";
richTextBox1.Text = "主线程线程ID:" + id + " 主线程名:" + Thread.CurrentThread.Name + "\n"; begin1 = new deltest1(method1);
callback = new AsyncCallback(back1); List<objstate> list = new List<objstate>();
for (int i = 0; i < 10; i++)
{
objstate obj = new objstate("James" + (i * i).ToString(), "Warke" + (i * 3).ToString());
list.Add(obj);
}
//delegate.BeginInvoke(parameter[] para, AsyncCallback callback, object obj)
//para是method方法的参数;
//callback是method方法执行完后在同一子线程中立即执行的回调函数;
//obj是一个可以传入回调函数中的object ,在回调函数中通过 IAsyncResult.AsyncState获取
begin1.BeginInvoke(back1, list); this.Location = new System.Drawing.Point(0, 0);
MessageBox.Show("主线程没有阻塞");
} private void back1(IAsyncResult ar)
{
begin1.EndInvoke(ar);
//回调函数中的线程ID与异步执行method时的线程ID相同,说明说明回调函数也是在子线程中执行
string id = Thread.CurrentThread.ManagedThreadId.ToString(); if (this.IsHandleCreated)
{
IAsyncResult iar = this.BeginInvoke(new Action(delegate()
{
richTextBox1.Text += "正在调用back函数 " + "当前线程ID:" + id + "\n";
}));
this.EndInvoke(iar);
}
List<objstate> list = (List<objstate>)(ar.AsyncState);//通过AsyncState获取传入的object
List<string> strList = new List<string>();
for (int i = 0; i < 10; i++)
{
Thread.Sleep(3000);
//winform中控件的属性设置或操作只能有主线程进行
//因此在子线程中需要调用 Control.BeginInvoke(Delegate method)方法 在主线程上对控件进行操作
//Control.BeginInvoke中传入的方法,应尽量只包含对控件操作的语句,这样主线程能够快速执行完对控件的操作,主界面不会假死
//Action<T>()是无返回值的泛型委托 Func<T>()是带返回值的泛型委托
//线程的执行有可能在窗口句柄创建完成前进行,此时会报错,因此 子线程中要异步操作主线程中的控件就需要在主线程的窗口句柄创建完成后进行
//句柄的类型是 IntPtr ,相当于windows中的身份证, 是一个指向指针的指针, 在内存中有固定的地址
//指针指向一块内存引用类或方法或程序集,不过内存中的地址会不断改变,系统需要通过一个指向指针的指针来记载数据地址的变更,便是句柄
if (this.IsHandleCreated)
{
IAsyncResult iar = this.BeginInvoke(new Action(delegate()
{
richTextBox1.Text += "正在调用back函数 " + "当前线程ID:" + id + " 正在给第" + (i + 1).ToString() + "个人赋值" + "\n";
}));
this.EndInvoke(iar);
}
DateTime now = DateTime.Now;
list[i].birthday = now;
string str = now.ToLongTimeString() + " " + list[i].fname + "." + list[i].lname;
strList.Add(str);
} begin2 = new deltest2(method2);
begin2.BeginInvoke(strList, back2, null);
}
private void back2(IAsyncResult ar)
{
//有返回值的方法,可以通过EndInvoke(IAsyncResult ar)方法获取返回值
string res = begin2.EndInvoke(ar);
//回调函数中的线程ID与异步执行method时的线程ID相同,说明说明回调函数也是在子线程中执行
string id = Thread.CurrentThread.ManagedThreadId.ToString(); if (this.IsHandleCreated)
{
IAsyncResult iar = this.BeginInvoke(new Action(delegate()
{
richTextBox1.Text += "正在调用back2函数 " + "当前线程ID:" + id + "\n";
}));
this.EndInvoke(iar); }
MessageBox.Show(res);
}
private void method1()
{
string id = Thread.CurrentThread.ManagedThreadId.ToString();
for (int i = 5; i > 0; i--)
{
Thread.Sleep(1000);
if (this.IsHandleCreated)
{
IAsyncResult iar = this.BeginInvoke(new Action(delegate()
{
richTextBox1.Text += "正在调用method方法 " + "当前线程ID:" + id + " " + i.ToString() + "秒后method方法,进入回调函数\n";
}));
this.EndInvoke(iar);
}
}
}
private string method2(List<string> list)
{
string id = Thread.CurrentThread.ManagedThreadId.ToString();
for (int i = 0; i < 10; i++)
{
Thread.Sleep(3000);
if (this.IsHandleCreated)
{
IAsyncResult iar = this.BeginInvoke(new Action(delegate()
{
richTextBox1.Text += "正在调用method2方法 " + "当前线程ID:" + id + "\n";
richTextBox1.Text += list[i] + "\n";
richTextBox1.Select(richTextBox1.TextLength, 0);
}));
this.EndInvoke(iar);
}
}
return "finish";
}
#endregion
}
}
AsyncCallback IAsyncResult的更多相关文章
- 异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小总结)
http://www.cnblogs.com/panjun-Donet/archive/2009/03/03/1284700.html 让我们来看看同步异步的区别: 同步方法调用在程序继续执行之前需要 ...
- C# - 多线程 之 异步编程
异步编程 同步编程,请求响应模型,同步化.顺序化.事务化. 异步编程,事件驱动模型,以 Fire and Forget 方式实现. 异步编程模式 -§- 异步编程模型 (APM) 模式: IAsyn ...
- .Net组件程序设计之异步调用
.Net组件程序设计之异步调用 说到异步调用,在脑海中首先想到就是BeginInvoke(),在一些常用对象中我们也会常常见到Invoke()和BeginInvoke(), 要想让自己的组件可以被客户 ...
- C#异步方法的使用
from:http://www.myext.cn/csharp/a_6765.html 也许业内很多高不成低不就的程序员都会对一些知识点会有些迷惑,原因是平常工作用的少,所以也就决定了你对这个事物的了 ...
- C#中的线程一(委托中的异步)
C#中的线程一(委托中的异步) 一.同步委托 我们平时所用的委托以同步居多,我们编写一个方法和相关委托进行演示: publicdelegatevoid DoSomethingDelegate(stri ...
- 介绍开源的.net通信框架NetworkComms框架 源码分析(十四)StreamTools
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...
- (转) C#如何使用异步编程
怎么使用异步,就是用委托进行处理,如果委托对象在调用列表中只有一个方法,它就可以异步执行这个方法.委托类有两个方法,叫做BeginInvoke和EndInvoke,它们是用来异步执行使用. 异步有三种 ...
- C#如何使用异步编程
怎么使用异步,就是用委托进行处理,如果委托对象在调用列表中只有一个方法,它就可以异步执行这个方法.委托类有两个方法,叫做BeginInvoke和EndInvoke,它们是用来异步执行使用. 异步有三种 ...
- Socket Programming in C#--Getting Started
Getting Started You can argue that one can overcome these shortcomings by multithreading meaning tha ...
随机推荐
- PHP FILTER_VALIDATE_FLOAT 过滤器
定义和用法 FILTER_VALIDATE_FLOAT 过滤器把值作为浮点数来验证. Name: "float" ID-number: 259 实例 <?php $var=1 ...
- 数学思维——cf351A
把每个值的各种贡献算一下即可 /* ai的小数部分为xi,向下取整对答案贡献为xi 向上取整对答案的贡献是xi-1,如果这个数是0,那么对答案的贡献是xi,即如果0向上取整就可以免去-1 然后sum{ ...
- css样式总结体会
css属性值语法:https://developer.mozilla.org/zh-CN/docs/Web/CSS/Value_definition_syntax 1.margin-top属性不起作用 ...
- centos7下命令行配置nginx
本教程中的步骤要求用户拥有root权限 第一步 - 添加Nginx存储库要添加CentOS 7 EPEL仓库,请打开终端并使用以下命令: sudo yum install epel-release第二 ...
- VC内联汇编,引用程序中的变量
int a=5; //变量a _asm { mov eax,a; //将变量a的值放入寄存器eax add eax,eax; //相当于a=a+a mov a,eax; // ...
- Java控制台
Console类的目的是使Java程序和控制台之间的交互更容易.Console类是java.io包中的一个实用程序类,用于访问系统控制台.控制台不能保证在所有机器上的Java程序中可访问. 例如,如果 ...
- vue组件的inheritAttrs属性
vue官网对于inheritAttrs的属性解释:如果你不希望组件的根元素继承特性,你可以在组件的选项中设置 inheritAttrs: false. 可能不是很好理解,我们可以举个例子来验证一下. ...
- 注册页面-使用form模块搭建
基于Django的form模块,快速的搭建注册页面,每个限制条件,都放在form模块里面,不单独对每一项编写标签,使用模版的 for 循环来渲染. 首先设置form模块 在blogs模块下创建一个bl ...
- winfrom创建转圈等待窗体
第一步:创建一个WaitForm public partial class WaitForm : Form { ; private ArrayList images = new ArrayList() ...
- 自记录:git如何上传文档到git@osc
前提: D盘有gitserver文件夹 双击桌面的git.exe文件,打开git命令窗口 输入cd d: 命令进入D盘 输入cd gitserver命令进入 找到git@osc自己参与项目里的htt ...