MethodInvoker和Invoker
来源:互联网 发布:网络彩票牌照 编辑:程序博客网 时间:2024/06/18 18:09
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Windows.Forms;namespace SQL_browser{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { /* * 1.MethodInvoke和Action都是方法返回类型为空的委托. * 2.MethodInvoker的原型定义为---> public delegate void MethodInvoker(); */ //Delegate d1 = (MethodInvoker)delegate { }; //Delegate d2 = (MethodInvoker)(() => { }); //Delegate d3 = (Action)delegate { }; //Delegate d4 = (Action)(() => { }); Thread t = new Thread(() => { Enumerable.Range(1, 10).ToList().ForEach(x => { if (listBox1.InvokeRequired) { listBox1.Invoke((Action)(() => { listBox1.Items.Add(string.Format("[{0}] executed 【 (Action)(() => {{ }}) 】", x)); })); } }); Enumerable.Range(11, 10).ToList().ForEach(x => { if (listBox1.InvokeRequired) { listBox1.Invoke((Action)delegate { listBox1.Items.Add(string.Format("[{0}] executed 【 (Action)delegate {{ }} 】", x)); }); } }); Enumerable.Range(21, 10).ToList().ForEach(x => { if (listBox1.InvokeRequired) { listBox1.CCInvoke(() => //使用lambda表达式 { listBox1.Items.Add(string.Format("[{0}] executed 【 lambda 】", x)); }); listBox1.CCInvoke(delegate //使用委托 { listBox1.Items.Add(string.Format("[{0}] executed 【 delegate 】", x)); }); } }); }); t.Start(); } } private void button2_Click(object sender, EventArgs e) { listBox1.Items.Clear(); Task taskWithFactoryAndState1 = Task.Factory.StartNew<List<int>>((stateObj) => { List<int> ints = new List<int>(); for (int i = 0; i < (int)stateObj; i++) { ints.Add(i); } return ints; }, 100).ContinueWith(ant => { listBox1.DataSource = ant.Result; }, TaskScheduler.FromCurrentSynchronizationContext()); } public static class ControlExtend { public static void CCInvoke(this Control control, Action action) { if (control.IsDisposed) return; try { control.Invoke((Delegate)action); } catch (ObjectDisposedException ode) { } catch (InvalidOperationException iox) { } } }}</int></int></int>
注意:button2_Click的方法会阻塞UI线程,如果数据量大或者里面的方法很耗时,界面会有卡死假象,比如窗体拖不动
0 0
- MethodInvoker和Invoker
- MethodInvoker
- MethodInvoker 委托
- Spring发布和使用Http Invoker
- 存储过程中definer和invoker
- 第二章 C和P,Invoker和Exporter
- Spring整合hessian和burlap及自带的Invoker
- 存储过程中-definer和invoker的解释
- 一个关于Definer和Invoker的权限问题
- tomcat invoker
- Dubbo Invoker
- Invoker HDU
- http invoker
- VS C++ MethodInvoker 倒数计时器
- C# 中MethodInvoker的理解
- Atitit.基于dsl的methodinvoker
- hession、burlap和http invoker远程调用方案的使用和比较
- 跨线程调用控件之MethodInvoker
- 实现js的trim()方法
- leetcode 116: Populating Next Right Pointers in Each Node
- truncate table 之后id列的处理,重置ID列
- Hdu oj 1019 Least Common Multiple
- iOS开发问题——UITextField弹出PickerView方法
- MethodInvoker和Invoker
- IPhone上应用及数据所在的目录
- 关于PDF转换成TXT转换器的运用
- mysql高可用之MHA(补充2)--邮件报警
- java.util.Date、java.sql.Date、java.sql.Time、java.sql.Timestamp区别和联系
- 优势与应用:再谈CDN镜像加速技术
- 一点点bat 批处理文件 总结
- Unity3D研究院之共享材质的巧妙用法(修改renderQueue 必须看看这个)
- JSP页面间的参数传递