今天写不完,明天会接着写的,,,,

学习C#有一段时间了,不过C#的委托+回调才这两天才会用,以前只是知道怎么用.前面的一篇文章,函数指针,其实是为这个做铺垫的,说白了委托就相当于C语言中的函数指针,具体说用委托有什么好处,我也不知道,但是因为你只要学C#那么回调就一定要会,回调是委托的一种.回调多用于线程间....还是用代码一点点的说明吧!现在认为自己以前不会用委托是因为手太懒,再者没有太多的必须用C#做的Demo,自己学东西都是用到什么学什么,想做什么东西需要什么知识就学什么,前几天有了必须要做的C#的Demo,关于检测TCP通信发过来的数据的.扯了这么多...回调主要的应用是---如果你在一个线程里操作像文本框,按钮,Label等组件时,会报错...原因--C#它不让这样操作,,,,

看例子

窗体里面就放一个textbox

先这样写

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 @delegate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "";
}
}
}

结果

就这一句   textBox1.Text = "123456";往文本框中写入123456     程序运行没问题

现在加入线程

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 @delegate
{
public partial class Form1 : Form
{
Thread writeThread;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
writeThread = new Thread(write_Metod);
writeThread.Start();
}
private void write_Metod()
{
textBox1.Text = "";
}
}
}

然后启动

蹦了

一起动就退出

说早了.......先说委托

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 @delegate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
write_Metod();
}
private void write_Metod()
{
textBox1.Text = "";
}
}
}

这个是没有用委托的,简单的例子,程序一运行就执行write_Metod();函数然后文本就打印123456了

现在就用委托来写一下

函数名字太长会让人感觉特复杂有木有....所以下面的名字.....

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 @delegate
{
public partial class Form1 : Form
{
delegate void a();//定义一个委托111111111111111111 public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
a b= new a(c);//实例化2222222222222222222
b();//3333333333333333333333
}
void c()
{
textBox1.Text = "";
}
}
}

 delegate void a();//定义一个委托111111111111111111
这是定义一个委托 要用关键字 delegate
后面的 void a();
函数名字自己取,,不过要有自己的规范,,有自己的一套编程风格就好..
void a();
这是一个没有返回值的,无参数的函数
因为学过函数指针都知道,只有类型一样的函数才能把一个函数的指针(地址)传给另一个函数..
因为我们希望把
void c()这个函数用另一个函数代替
这个函数的类型是void 的 无参数的函数 所以就这样定义了
delegate void a();//定义一个委托----再次说明delegate是关键字,意味着定义了一个委托------你说函数指针也行,,C#啊;;;淡化了指针的概念

然后
a b= new a(c);//实例化2222222222222222222

不知道有没有不知道为何实例化的
如果不知道实例化那么知道对象吗?是C#整的名词对象
如果不知道对象那么知道类吗?,,,,,
上面所说的没有什么用的,只是用来吓唬人的,记得第一次想学高级语言,,,一打开书,,,,,崩溃了,彻底崩溃了,什么对象,,,,一开头介绍就是,,,什么面向对象,,吓的我赶紧把书方回去,,心有不甘再来一本,,没想到一打开书...又来了,,,XXXXXX是面向对象的语言,,,,,那时候在想,我去太高深了,,面向对象,,面对谁呢!! 毕向东的JAVA讲的不错....学会一门高级语言,语言有很多共通的地方
又耽误大家时间了....对了马士兵的JAVA也挺好,,,都看看 都看看
关于实例化
定义一个A a;假设定义了一个a
如果你不实例化也就是 a = new A();
那么系统不会为a开辟任何空间,只有 a = new A();了 系统才会在内存中为 a 开辟一段空间,才真正有了a的存在
a b= new a(c);//实例化2222222222222222222
这一句的意思就是
b= c;
哦,原来就是函数指针赋值啊
那么
b();   就相当于  c();
好再过一遍
先定义一个委托
delegate void a();//定义一个委托

然后实例化---a b = new a(c);
void c()
{ }
这样 b 就==c了
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 @delegate
{
public partial class Form1 : Form
{
delegate void a();//定义一个委托 public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
a b = new a(c);//实例化
b();
}
void c()
{
textBox1.Text = "";
}
}
}


咱接着

假如说

 void c(string str)
{
textBox1.Text = str;
}

那么我定义的委托(函数指针)也应该是

delegate void a(string str);//定义一个委托

怎样把c传过去呢

a b = new a(c);//实例化

然后

b("");

因为c是

void c(string str)
所以
必须写个string进去嘛
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 @delegate
{
public partial class Form1 : Form
{
delegate void a(string str);//定义一个委托 public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
a b = new a(c);//实例化
b("");
}
void c(string str)
{
textBox1.Text = str;
}
}
}

委托也就这样吧
下面看 回调 窗体不变

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 @delegate
{
public partial class Form1 : Form
{
Thread writeThread;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
writeThread = new Thread(c);
writeThread.Start();
}
private void c()
{
textBox1.Text = "";
}
}
}

然后启动

程序---崩了

好现在

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 @delegate
{
public partial class Form1 : Form
{
Thread writeThread;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;////////**************************加了这一句
writeThread = new Thread(c);
writeThread.Start();
}
private void c()
{
textBox1.Text = "";
}
}
}

满血复活了..

System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
上一次程序崩掉是因为C#不让跨线程调用窗体控件--不让在一个新的线程里调用窗体控件---
textBox1.Text = "123456";就是在使用窗体控件textbox
加上这句System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls
Check  For  Illegal  Cross  Thread  Calls  == false;   不检查
检查 对 非法的 交叉 线程 调用
所以就通过了---当自己写程序时调试可以使用,,真正做项目嘛,,,,,因人而异吧 C#提供了几种种方法来让我们在线程里来操作窗体控件---其它高级语言也提供了相应的方法的
看 回调 来也
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 @delegate
{
public partial class Form1 : Form
{
Thread writeThread;
delegate void a();//定义回调
a b;//声明回调
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
b = new a(d);//实例化回调 writeThread = new Thread(c);
writeThread.Start();//启动C线程
}
private void c()
{
textBox1.Invoke(b);
}
private void d()
{
textBox1.Text = "";
}
}
}
textBox1.Invoke(b);
其实就多了这一句
还可以这样
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 @delegate
{
public partial class Form1 : Form
{
Thread writeThread;
delegate void a(string str);
a b;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
b = new a(d); writeThread = new Thread(c);
writeThread.Start();
}
private void c()
{
textBox1.Invoke(b,"");
}
private void d(string str)
{
textBox1.Text = str;
}
}
}
textBox1.Invoke(b,可以带参数);//这是固定形式,就是这样用,,,人家规定的
现在放一个按钮

一启动

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 @delegate
{
public partial class Form1 : Form
{
Thread writeThread;
delegate void a(string str);
a b;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
b = new a(d); writeThread = new Thread(c);
writeThread.Start();
}
private void c()
{
button1.Invoke(b, "");
}
private void d(string str)
{
button1.Text = str;
}
}
}

还是这样用

就写这些吧!简简单单普普通通,,,现在博客园终于能复制粘贴图片了........太方便了

C#委托+回调详解的更多相关文章

  1. C#委托使用详解(Delegates)

    摘要 委托是C#编程一个非常重要的概念,也是一个难点.本文将系统详细讲解委托. 1. 委托是什么? 其实,我一直思考如何讲解委托,才能把委托说得更透彻.说实话,每个人都委托都有不同的见解,因为看问题的 ...

  2. javascript事件委托机制详解

    以个人前端工作面试经历来看,javascript事件委托是问的最多的一类题目之一,熟悉事件委托能够了解你对于javascript的掌握程度. 面试官可能问一下问题,现在有5个li待办事件,需要实现当点 ...

  3. Android回调详解

         很多时候开发遇到一些Ui更新 网络数据获取,或者方法方法传递的时候会借助回调函数,那么什么是回调函数 百度百科是这么解释的  转载请标注出处 http://blog.csdn.net/sk7 ...

  4. 基于接口回调详解JUC中Callable和FutureTask实现原理

    Callable接口和FutureTask实现类,是JUC(Java Util Concurrent)包中很重要的两个技术实现,它们使获取多线程运行结果成为可能.它们底层的实现,就是基于接口回调技术. ...

  5. C#中常见的系统内置委托用法详解(抄录)

    C#中常见的系统内置委托 Action类.Func类.Predicate<T>.Comparison<T>委托 Action类的委托 Action委托 封装一个方法,该方法不具 ...

  6. 【C#】C#委托使用详解(Delegates)

    摘要 委托是C#编程一个非常重要的概念,也是一个难点.本文将系统详细讲解委托. 1. 委托是什么? 其实,我一直思考如何讲解委托,才能把委托说得更透彻.说实话,每个人都委托都有不同的见解,因为看问题的 ...

  7. Java内部类之间的闭包和回调详解

    前言 闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域.通过这个定义,可以看出内部类是面向对象的闭包,因为它不仅包含外围类对象(创建内部类的作用域)的信息,还自 ...

  8. 可观测委托与map委托原理详解

    在上一次https://www.cnblogs.com/webor2006/p/11369333.html中学习了委托属性,然后它在实际中有四种使用情况: 接下来则学习一下另外两种属性委托的使用. 可 ...

  9. Kotlin枚举与委托深入详解

    枚举: 基本上跟Java的差不多,这里就过一遍既可,如下: 还可以接收参数,如下: 枚举还可以定义方法,如下: 看下错误提示: 所以可以这样: 然后咱们再冒号之前定义对象,如下: 下面来使用一下: 当 ...

随机推荐

  1. IO流作业

    IO流作业 一.    填空题 Java IO流可以分为    字节流          和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行. 输入流的唯一目的是提供通往数据的通道 ...

  2. webpack打包小图片时进行Base64转码

    关于base64 优点: base64就是一串字符串码表示的图片,在加载页面和js时一块加载出来,减少了加载图片时的http请求.加载一张图片时会发起一次http请求,http请求每次建立都会需要一定 ...

  3. Javascript 随机数函数 学习之二:产生服从正态分布随机数

    一.为什么需要服从正态分布的随机函数 一般我们经常使用的随机数函数 Math.random() 产生的是服从均匀分布的随机数,能够模拟等概率出现的情况,例如 扔一个骰子,1到6点的概率应该相等,但现实 ...

  4. vue中prop传值时加不加v-bind(冒号:)

    前言:有关Vue中父组件通过prop传值给子组件时,是否加v-bind的问题,没弄清楚时感觉很乱,弄清楚之后很简单. 由于结果记起来很容易,所以先给出结果: 只有传递字符串常量时,不采用v-bind形 ...

  5. AngularJS学习之 angular-file-upload控件使用方法

    1.官方链接 https://github.com/nervgh/angular-file-upload 2.安装到项目中 bower install angular-file-upload(安装完成 ...

  6. 活字格Web应用平台学习笔记 7 - 导出 Excel

    活字格一直强调和Excel的兼容,可以导入导出Excel,今天终于学到这一课了. 课程目标: 好吧,就是这么快,已经加了一个“导出到Excel”的按钮了 我以为多高深呢,原来人家都给写好逻辑了,直接选 ...

  7. [我的阿里云服务器] —— WorkPress

    前言: WordPress是基于PHP和MYSQL编成的一套博客系统,因此一般会选择LAMP环境来让它最稳定地运行, 这里的LAMP指的是Linux.Apache.MySQL.PHP,我们我的阿里云服 ...

  8. 在Docker Swarm上部署Apache Storm:第1部分

    [编者按]本文来自 Baqend Tech Blog,描述了如何在 Docker Swarm,而不是在虚拟机上部署和调配Apache Storm集群.文章系国内 ITOM 管理平台 OneAPM 编译 ...

  9. 基于MSMQ绑定的WCF服务实现总结

    一. 创建消息队列    1 1) 创建一个非事物性的私有队列    1 2)设置消息队列访问权限    2 二.创建WCF服务并绑定消息队列    4 1)创建HelloService服务    4 ...

  10. .NET笔试题集(五)

    转载于:http://www.cnblogs.com/ForEvErNoME/archive/2012/09/15/2684938.html 1.什么是受管制的代码? 答:unsafe:非托管代码.不 ...