番外篇 之 C#委托
对于上一节 番外篇之C#多线程的反思
反思一: Thread th = new Thread(参数); ////参数的总结
////首先,第一情况,对于 Thread th = new Thread(showLable);中的showLable()方法是无参的方法,则是调用了第二个重载的方法:public Thread(ThreadStart start);,无参数,直接创建实例。补充:public delegate void ThreadStart();
private void showLable()
{
//labResult.Text = res.ToString();
}
private void btnGetName_Click(object sender, EventArgs e)
{
Thread th = new Thread(showLable);
th.Start();
}
////第二种情况,则是使用了第一种重载方法: public Thread(ParameterizedThreadStart start);先创建实例,在启动的时候,再传递参数,但是由于 thread.Start((object)strinfo);只有一个有参数的重载方法,而且只能是object类型,则直接导致了,GetInfo(object strinfo)传递参数类型只能是object类型。补充: public delegate void ParameterizedThreadStart(object obj);
private void GetInfo(object strinfo)
{
MessageBox.Show(strinfo.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
string strinfo = "Andrew";
Thread thread = new Thread(GetInfo);
thread.Start((object)strinfo);
}
反思二:Thread thread = new Thread(new ThreadStart(delegate { GetInfo((object)strinfo, strinfo); }));
关于匿名方法的使用:参考链接 http://www.cnblogs.com/Andrew2014/p/4074667.html
视频一:委托的介绍以及委托的语法及其用法
1. 委托是什么?
委托,委派一个人托付一件事。
是委托一个变量,一个方法(一件事)。
将方法当做参数传递
2. 为什么要有委托?
实例:房子中介
3. 委托能干嘛呢?
4.为什么委托跟指针一样?
委托和C++中的指针一样: 指针是指向内存地址;委托是“指向”方法的内存地址。
5.委托的应用场景
在类中我需要传递给窗体一个信息,但是不想让这个信息以返回值的形式返回。
常规情况使用委托场景:
1.多线程中解决由“线程间操作无效:从不是创建控件“xxxx”的线程访问它。”
2.异步模式 (必须用委托,用事件也行)
6.多播委托
this.Invoke(_GetInfo,new object []{hellow});
额外的知识 this 关键字
标识当前活动的对象,如果在窗体中,那么可以表示当前的Form
如果在事件中,表示当前激活事件的宿主。
委托的语法
权限修辞符 委托关键字 委托返回值 委托名([参数])
public delegate void GetInfo();////定义委托,相当于定义一个类。
权限修辞符 委托 变量名
public GetInfo getInfo;////声明委托变量,相当于声明一个类的实例。
直接和方法调用一样,传递变量即可
getInfo(str);////委托的第一种使用方式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms; namespace _2014_11_05_01番外篇之委托
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnGetName_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(delegate
{
DelegateDemoClass ddcl = new DelegateDemoClass();
ddcl.getInfo = new GetInfo(showLable);////初始化委托变量
string name = txtName.Text;////跨线程异常,只针对给控件赋值,如果只是读取数据,不存在异常。
string hellow = ddcl.Hellow(name);
showLable(hellow);
}));
} private void showLable(string res)
{
labResult.Text = res;////如何修复跨线程调用异常
}
}
}
using System;
using System.Collections.Generic;
using System.Text; namespace _2014_11_05_01番外篇之委托
{
/// <summary>
/// 无返回值,无参数委托
/// </summary>
public delegate void GetInfo(string res);////定义委托 public class DelegateDemoClass
{
public GetInfo getInfo;////声明委托变量
public string Hellow(string name)
{
string str = "Hellow " + name;
getInfo(str);////委托的第一种使用方式,直接和方法调用一样,传递变量即可。
return str;
//return "Hellow "+name;
}
}
}
视频二:番外篇之委托-02委托的学习几大误区以及解决跨线程访问异常的方案
问题一:为什么使用委托以后,还会出现跨线程访问的错误.
误区:委托仅仅是将方法当做参数传递,“并不是用来执行或者解决一些特殊的问题,比如说跨线程异常”。
解决方案:使用Invoke关键字, this.Invoke(_GetInfo,new object []{hellow});
从创建窗口控件的线程上,用指定的参数列表执行指定委托
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms; namespace _2014_11_05_01番外篇之委托
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_GetInfo = new GetInfo(showLable);
} GetInfo _GetInfo;////在主窗体也声明一个委托的变量 private void btnGetName_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(delegate()
{
DelegateDemoClass ddcl = new DelegateDemoClass();
ddcl.getInfo = new GetInfo(showLable);////正确的,直接初始化委托变量
//ddcl.getInfo = _GetInfo;////也是正确的,在内部声明委托,赋值给外部委托
string name = txtName.Text;////跨线程异常,只针对给控件赋值,如果只是读取数据,不存在异常。
string hellow = ddcl.Hellow(name);
//showLable(hellow);
//_GetInfo(hellow);
this.Invoke(_GetInfo,new object []{hellow});
}));
th.Start();
} private void showLable(string res)
{
labResult.Text = res;////如何修复跨线程调用异常
} }
} using System;
using System.Collections.Generic;
using System.Text; namespace _2014_11_05_01番外篇之委托
{
/// <summary>
/// 无返回值,无参数委托
/// </summary>
public delegate void GetInfo(string res);////定义委托 public class DelegateDemoClass
{
public GetInfo getInfo;////声明委托变量
public string Hellow(string name)
{
string str = "Hellow " + name;
//getInfo(str);////委托的第一种使用方式,直接和方法调用一样,传递变量即可。
return str;
//return "Hellow "+name;
}
}
}
视频三:03委托的注意事项以及多播委托
this.Invoke(); 推荐使用
原因:由于我的控件是”我”(Form1)创建的,那么最好还是由我来调用/销毁
在创建控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托 微软官方翻译
从创建控件的线程上,用指定的参数列表执行指定委托 君临翻译
Base.Invoke(); 越俎代庖.
在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托。微软官方翻译
从拥有控件的线程上,用指定的参数列表执行指定委托
多播委托又被称为“委托链”
多播委托其实就是一个委托接一个委托……
执行所有给他注册的方法.
+= 增加一个委托方法
-= 去除一个委托方法.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading; namespace 委托Demo
{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
GetInfo _GetInfo;//主窗体也声明这个委托的变量
private void Form1_Load(object sender, EventArgs e)
{
// = 赋值
_GetInfo = new GetInfo(showLable); _GetInfo += new GetInfo(showLable1);
// += 注册方法
_GetInfo += new GetInfo(showLable2);
} private void btnGetName_Click(object sender, EventArgs e)
{
#region 基础的委托使用
//Thread th = new Thread(new ThreadStart(delegate
//{
// DelegateDemoClass ddcl = new DelegateDemoClass();
// //ddcl.getInfo = _GetInfo; 在内部声明委托,赋值给外部委托.
// ddcl.getInfo = new GetInfo(showLable); //正确的.直接初始化委托
// string name = txtName.Text;
// string hellow = ddcl.Hellow(name);
// //_GetInfo(hellow);
// //this.Invoke(ddcl.getInfo, new object[] { hellow }); 等同于 this.Invoke(new Action<string>(showLable));
// //showLable(hellow);
//}));
//th.Start();
#endregion _GetInfo -= new GetInfo(showLable1);
DelegateDemoClass ddcl = new DelegateDemoClass();
string name = txtName.Text;
string hellow = ddcl.Hellow(name);
_GetInfo(hellow); //this.Invoke(ddcl.getInfo, new object[] { hellow }); 等同于 this.Invoke(new Action<string>(showLable));
//showLable(hellow); }
private void showLable2(string res)
{
MessageBox.Show(res + "showLable2");
}
private void showLable1(string res)
{
MessageBox.Show(res + "showLable1");
}
private void showLable(string res)
{
this.Invoke(new ThreadStart(delegate
{
labResult.Text = res; //如何修复跨线程调用异常.
}));
//this.Invoke 是不可以使用lambda表达式.
//Thread th = new Thread(p => { //});
//ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { //}));
//ThreadPool.QueueUserWorkItem(p => { //}); }
}
} using System;
using System.Collections.Generic;
using System.Text; namespace 委托Demo
{
/// <summary>
/// 无返回值,无参委托
/// </summary>
public delegate void GetInfo(string res); //声明委托
public class DelegateDemoClass
{
public GetInfo getInfo; //声明委托变量
public string Hellow(string name)
{
string str="Hellow " + name;
//getInfo(str);//委托的第一种使用方式
return str;
} }
}
视频教程出自:http://www.xuanjics.com/thread-79-1-1.html
玄机论坛的地址:www.xuanjics.com 原创作者:君临
番外篇 之 C#委托的更多相关文章
- 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- iOS冰与火之歌(番外篇) - 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权
iOS冰与火之歌(番外篇) 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权 蒸米@阿里移动安全 0x00 序 这段时间最火的漏洞当属阿联酋的人权活动人士被apt攻击所使用 ...
- 给深度学习入门者的Python快速教程 - 番外篇之Python-OpenCV
这次博客园的排版彻底残了..高清版请移步: https://zhuanlan.zhihu.com/p/24425116 本篇是前面两篇教程: 给深度学习入门者的Python快速教程 - 基础篇 给深度 ...
- 可视化(番外篇)——在Eclipse RCP中玩转OpenGL
最近在看有关Eclipse RCP方面的东西,鉴于Gephi是使用opengl作为绘图引擎,所以,萌生了在Eclipse RCP下添加画布,使用opengl绘图的想法,网上有博文详细介绍这方面的内容, ...
- 可视化(番外篇)——SWT总结
本篇主要介绍如何在SWT下构建一个应用,如何安装SWT Designer并破解已进行SWT的可视化编程,Display以及Shell为何物.有何用,SWT中的常用组件.面板容器以及事件模型等. 1.可 ...
- 【重走Android之路】【番外篇】关于==和equals
[重走Android之路][番外篇]关于==和equals 在实际的编程当中,经常会使用==和equals来判断变量是否相同.但是这两种比较方式也常常让人搞得云里雾里摸不着头脑.下面是我个人做的总 ...
- 【重走Android之路】【番外篇】有关于null的一些知识点
[重走Android之路][番外篇]有关于null的一些知识点 1.首先,到底什么是null? null是Java中的一个关键字,用于表示一个空对象引用,但其本身并不是任何类型也不是属于任何对象. ...
- [置顶] think in java interview番外篇-谈程序员如何修练英语
一.程序员对英语能力的重视度和能力要求应该是在各行各业中排在比较靠前的 这样说吧,英语程度的好坏直接影响着一个程序员的编程.开发.创新能力. 道理很简单: 1. 计算机和软件是用英语创造出来的 2. ...
- python自动化测试应用-番外篇--接口测试1
篇1 book-python-auto-test-番外篇--接口测试1 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),至今<安卓a ...
随机推荐
- 新建VM_Script
在Hyper-V群集中,不需要设置VM的自启动,当宿主机意外关机重新启动后,上面的VM会自动转移到另一台主机:如果另一台主机处于关机状态,则宿主机重新启动后,其VM也会自启动(如果其VM在宿主机关机前 ...
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana 分块
E. GukiZ and GukiZiana Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...
- bzoj 1026 [SCOI2009]windy数 数位dp
1026: [SCOI2009]windy数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline ...
- 分享:Android中利用机器码注册机制防止破解(转)
转自:http://blog.csdn.net/huzgd/article/details/6684094 最近做一个Android应用时遇到这个问题,客户要求功能必须注册才能使用,而程序本身又不是联 ...
- ios开发——实用技术篇Swift篇&播放MP3
播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...
- 认清Linux中标准输入和标准输出的双重含义
按照惯例,UNIX系统shell使用文件描述符0与进程的标准输入(一般是键盘)相关联,文件描述符1与标准输出(一般是显示器)相关联,文件描述符2与标准出错输出(一般是显示器)相关联. 在依从POSIX ...
- ubuntu 11.10 安装apache2 tomcat6
ubuntu 11.10 安装apache2 tomcat6 导读 Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目 ...
- 经典的iptables shell脚本
PS:这个iptables脚本不错,很实用,根据实际应用改一下就可以自己用.分享出来,供大家来参考.原作者佚名.源代码如下: #!/bin/sh modprobe ipt_MASQUERADE mod ...
- iOS 抓取 UIwebview 上 所有 图片 并进行滚动播放
关于在UIwebview上添加滚动图片 两种滚动手势会混淆,应为webview有webview.scrollview的属性 故参照昨天的随笔 scrollview嵌套解决方案. 本篇随笔主要讲循环使用 ...
- Android进阶笔记18:选用合适的IPC方式
1. 相信大家都知道Android进程间通信方式很多,比如AIDL.Messenger等等,接下来我就总结一下这些IPC方式优缺点. 2. IPC方式的优缺点和适用场景 3. 附加:使用Intent实 ...