对于上一节 番外篇之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  原创作者:君临

                    QQ交流群:16885911  

番外篇 之 C#委托的更多相关文章

  1. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  2. iOS冰与火之歌(番外篇) - 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权

    iOS冰与火之歌(番外篇) 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权 蒸米@阿里移动安全 0x00 序 这段时间最火的漏洞当属阿联酋的人权活动人士被apt攻击所使用 ...

  3. 给深度学习入门者的Python快速教程 - 番外篇之Python-OpenCV

    这次博客园的排版彻底残了..高清版请移步: https://zhuanlan.zhihu.com/p/24425116 本篇是前面两篇教程: 给深度学习入门者的Python快速教程 - 基础篇 给深度 ...

  4. 可视化(番外篇)——在Eclipse RCP中玩转OpenGL

    最近在看有关Eclipse RCP方面的东西,鉴于Gephi是使用opengl作为绘图引擎,所以,萌生了在Eclipse RCP下添加画布,使用opengl绘图的想法,网上有博文详细介绍这方面的内容, ...

  5. 可视化(番外篇)——SWT总结

    本篇主要介绍如何在SWT下构建一个应用,如何安装SWT Designer并破解已进行SWT的可视化编程,Display以及Shell为何物.有何用,SWT中的常用组件.面板容器以及事件模型等. 1.可 ...

  6. 【重走Android之路】【番外篇】关于==和equals

    [重走Android之路][番外篇]关于==和equals   在实际的编程当中,经常会使用==和equals来判断变量是否相同.但是这两种比较方式也常常让人搞得云里雾里摸不着头脑.下面是我个人做的总 ...

  7. 【重走Android之路】【番外篇】有关于null的一些知识点

    [重走Android之路][番外篇]有关于null的一些知识点   1.首先,到底什么是null? null是Java中的一个关键字,用于表示一个空对象引用,但其本身并不是任何类型也不是属于任何对象. ...

  8. [置顶] think in java interview番外篇-谈程序员如何修练英语

    一.程序员对英语能力的重视度和能力要求应该是在各行各业中排在比较靠前的 这样说吧,英语程度的好坏直接影响着一个程序员的编程.开发.创新能力. 道理很简单: 1. 计算机和软件是用英语创造出来的 2. ...

  9. python自动化测试应用-番外篇--接口测试1

    篇1                 book-python-auto-test-番外篇--接口测试1 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),至今<安卓a ...

随机推荐

  1. 如何自学Java 经典

    JAVA自学之路 JAVA自学之路 一:学会选择 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多数人选的是java,或是.net,也有一些选择了手机.嵌入式.游戏.3G.测试等. 那么究竟 ...

  2. Java浮点值拒绝服务漏洞危害分析

    By 空虚浪子心 http://www.inbreak.net/ JAVA出了漏洞,CVE-2010-4476,会导致拒绝服务攻击.大家能从公告上,看到这样一段代码,挺长的.意思是只有开发人员写出这样 ...

  3. Swift学习笔记十五

    自动引用计数(Automatic Reference Counting) 和OC一样,Swift用自动引用计数机制来跟踪和管理你应用程序的内存,大多数情况下,你不需要考虑自己管理内存,Swift会自动 ...

  4. Codeforces Round #306 (Div. 2) A. Two Substrings 水题

    A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...

  5. js实现按回车自行提交

    <script type="text/javascript"> document.onkeydown = function (e) { var theEvent = w ...

  6. android一些系统相关的东西

    添加快捷方式和删除快捷方式: private void addShortcut() { Intent shortcut = new Intent( "com.android.launcher ...

  7. [终极精简版][图解]Nginx搭建flv mp4流媒体服务器

    花了我接近3周,历经了重重问题,今日终于把流媒体服务器搞定,赶紧的写个博文以免忘记... 起初是跟着网上的一些教程来的,但是说的很不全面,一些东西也过时不用了(比如jwplayer老版本).我这次是用 ...

  8. 凸包---HDU 2202

    题意:给N个点,求着N个点中选择三个联的最大的三角形面积! 注意精度:不然OJ上面会超时的 #include<iostream> #include<cmath> #includ ...

  9. percona-xtrabackup 文档

    https://www.percona.com/doc/percona-xtrabackup/2.4/index.html

  10. linux 体系结构知识 博客

    http://blog.csdn.net/haiross/article/category/1488205/3