番外篇 之 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 ...
随机推荐
- PostgreSQL的AnynonArray的例子
程序: CREATE OR REPLACE FUNCTION kappend(anynonarray, anyelement) RETURNS text AS $$ ; $$ LANGUAGE SQL ...
- iOS 本地企业发布流程
今天和后台弄了一下本地企业ipa发布, 准备工具:要发布的ipa文件一个,一个plist plist文件格式内容: <?xml version="1.0" encoding= ...
- 正则表达式_删除字符串中的任意空格(Regex)
直接用 -split,默认以空白分隔.-split $a 用正则表达式中的 \s,-replace -split中都可以直接使用正则表达式,select-string也可以 split 和 join ...
- ASP.net中GridView中增加一行记录并默认显示为编辑状态
//添加 protected void Button1_Click(object sender, EventArgs e) { DataSet ds = (DataSet)pa.GetDataSet( ...
- [MySQL复制异常]'Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.'
MySQL复制错误]Last_Errno: 1666 Last_Error: Error executing row event: 'Cannot execute statement: imposs ...
- Codeforces Round #250 (Div. 2)——The Child and Set
题目链接 题意: 给定goal和limit,求1-limit中的若干个数,每一个数最多出现一次,且这些数的lowbit()值之和等于goal,假设存在这种一些数,输出个数和每一个数:否则-1 分析: ...
- 放肆的使用UIBezierPath和CAShapeLayer画各种图形
CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形,当然,你也可以使用其他方式来画,随你. 杂谈 在 CAShapeLayer 中,也可以像 CAL ...
- 云服务器 ECS Linux 磁盘空间满(含 innode 满)问题排查方法
问题描述 在云服务器 ECS Linux 系统内创建文件时,出现类似如下空间不足提示: No space left on device … 问题原因 导致该问题的可能原因包括: 磁盘分区空间使用率达到 ...
- 文件系统缓存dirty_ratio与dirty_background_ratio两个参数区别
这两天在调优数据库性能的过程中需要降低操作系统文件Cache对数据库性能的影响,故调研了一些降低文件系统缓存大小的方法,其中一种是通过修改/proc/sys/vm/dirty_background_r ...
- Js 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
filter(): 语法: var filteredArray = array.filter(callback[, thisObject]); 参数说明: callback: 要对每个数组元素执行 ...