特性一:委托

委托是C#语言中特有的概念,相当于C/C++中的函数指针,与C/C++中函数指针的不同之处是:委托是面向对象的、类型安全的和保险的,是引用类型。因此,对委托的使用要

“先定义、后声明,接着实例化、然后作为参数传递给方法,最后才能使用”。

1、定义委托使用关键字delegate:

delegate  void SomeDelegate(type1 para1,......typen paran);

2、声明委托:

SomeDelegate  d;

3、实例化委托:

d=new SomeDelegate(obj.InstanceMethod);

其中obj是对象,InstanceMethod是它的实例方法。

4、作为参数传递给方法

someMethod(d);

5、最后在此方法的实现代码中使用委托

private  void  someMethod(SomeDelegate  someDelegate)

{

.....

//使用委托

someDelegate(arg1,arg2,....,argn);

......

}

通过委托SomeDelegate实现对方法InstanceMethod的调用,调用还必须有一个前提条件:方法InstanceMethod有参数且和定义SomeDelegate的参数一致,并且返回类型相同(本例中为void)。方法InstanceMethod的定义:

private  void  InstanceMethod(type1 para1,type2 para2,......,typen paran)

{

//方法体

.....

}

委托的实例化中的参数既可以是实例方法,也可以是静态方法。

使用委托实现“文字抄写员”的小程序,界面如下:

在下方文本框中编辑文字,勾选“书写到”组框中的“文本区1”和(或)“文本区2”复选框后单击“提交”按钮,程序会自动将文本框中的文字“抄写”到对应的用户勾选的文本区中去。

代码实现如下:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace DelegateDemo
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
} //1、定义委托
private delegate void WriteToTextBox(string strTxt);
//2、声明委托
private WriteToTextBox writeToTextBox; /// <summary>
/// 提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_OK_Click(object sender, EventArgs e)
{
if (chbOne.Checked)
{
gbJobOne.Text = "运行中......";
gbJobOne.Refresh();
txtJobOne.Clear();
//3、实例化委托
writeToTextBox = new WriteToTextBox(WriteTextBox1);
//4、将委托作为方法的参数进行传递
WriteText(writeToTextBox);
gbJobOne.Text = "任务1完成";
}
if (chbTwo.Checked)
{ gbJobTwo.Text = "运行中......";
gbJobTwo.Refresh();
txtJobTwo.Clear();
//3、实例化委托
writeToTextBox = new WriteToTextBox(WriteTextBox2);
//4、将委托作为方法的参数进行传递
WriteText(writeToTextBox);
gbJobTwo.Text = "任务2完成";
}
} private void WriteText(WriteToTextBox writeMethod)
{
string strData = this.txt_Input.Text;
writeMethod(strData);
}
private void WriteTextBox1(string strTxt)
{
this.txtJobOne.Text = strTxt;
} private void WriteTextBox2(string strTxt)
{
this.txtJobTwo.Text = strTxt;
} /// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_Load(object sender, EventArgs e)
{
//设置文本框获取焦点
this.ActiveControl = this.txt_Input;
//this.txt_Input.Focus();
}
}
}

特性2:多线程

多线程的具体介绍请参考博文:http://www.cnblogs.com/dotnet261010/p/6159984.html

使用多线程实现上一节的程序,代码如下:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;//引入多线程的命名空间 namespace DelegateDemo
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
} //1、定义委托
private delegate void WriteToTextBox(string strTxt);
//2、声明委托
private WriteToTextBox writeToTextBox; /// <summary>
/// 提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_OK_Click(object sender, EventArgs e)
{
//创建线程1
Thread thread1 = new Thread(new ThreadStart(ExecuteTsk1));
//启动线程1
thread1.Start(); //创建线程2
Thread thread2 = new Thread(new ThreadStart(ExecuteTsk2));
//启动线程2
thread2.Start(); } private void ExecuteTsk1()
{
if (chbOne.Checked)
{
gbJobOne.Text = "运行中......";
gbJobOne.Refresh();
txtJobOne.Clear();
//3、实例化委托
writeToTextBox = new WriteToTextBox(WriteTextBox1);
//4、将委托作为方法的参数进行传递
WriteText(writeToTextBox);
gbJobOne.Text = "任务1完成";
}
} private void ExecuteTsk2()
{
if (chbTwo.Checked)
{ gbJobTwo.Text = "运行中......";
gbJobTwo.Refresh();
txtJobTwo.Clear();
//3、实例化委托
writeToTextBox = new WriteToTextBox(WriteTextBox2);
//4、将委托作为方法的参数进行传递
WriteText(writeToTextBox);
gbJobTwo.Text = "任务2完成";
}
} private void WriteText(WriteToTextBox writeMethod)
{
string strData = this.txt_Input.Text;
writeMethod(strData);
}
private void WriteTextBox1(string strTxt)
{
this.txtJobOne.Text = strTxt;
} private void WriteTextBox2(string strTxt)
{
this.txtJobTwo.Text = strTxt;
} /// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_Load(object sender, EventArgs e)
{
//设置文本框获取焦点
this.ActiveControl = this.txt_Input;
//允许跨线程调用
Control.CheckForIllegalCrossThreadCalls = false;
}
}
}

特性3:C#方法回调

C#回调的具体介绍请参照博文:http://www.cnblogs.com/dotnet261010/p/6159984.html

使用委托、多线程和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.Tasks;
using System.Windows.Forms;
using System.Threading;//引入多线程的命名空间 namespace DelegateDemo
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
} //1、定义委托
private delegate void WriteToTextBox(string strTxt);
//2、声明委托
private WriteToTextBox writeToTextBox; //定义并声明操作文本区1的回调
private delegate void WriteTxtJobOneCallBack(string strValue);
WriteTxtJobOneCallBack writeTxtJobOneCallBack; //定义并声明操作文本区2的回调
private delegate void WriteTxtJobTwoCallBack(string strValue);
WriteTxtJobOneCallBack writeTxtJobTwoCallBack; //定义并声明操作"任务1"分组框的回调
private delegate void ShowGroupOneCallBack(string strValue);
ShowGroupOneCallBack showGroupOneCallBack; //定义并声明操作"任务2"分组框的回调
private delegate void ShowGroupTwoCallBack(string strValue);
ShowGroupOneCallBack showGroupTwoCallBack; /// <summary>
/// 提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_OK_Click(object sender, EventArgs e)
{
//创建线程1
Thread thread1 = new Thread(new ThreadStart(ExecuteTsk1));
//启动线程1
thread1.Start(); //创建线程2
Thread thread2 = new Thread(new ThreadStart(ExecuteTsk2));
//启动线程2
thread2.Start(); } private void ExecuteTsk1()
{
if (chbOne.Checked)
{
//3、实例化委托
writeToTextBox = new WriteToTextBox(WriteTextBox1);
//4、将委托作为方法的参数进行传递
WriteText(writeToTextBox);
//使用回调
this.gbJobOne.Invoke(showGroupOneCallBack, "任务1");
}
} private void ExecuteTsk2()
{
if (chbTwo.Checked)
{
//3、实例化委托
writeToTextBox = new WriteToTextBox(WriteTextBox2);
//4、将委托作为方法的参数进行传递
WriteText(writeToTextBox);
//使用回调
this.gbJobTwo.Invoke(showGroupTwoCallBack, "任务2");
}
} /// <summary>
/// 执行自定义委托
/// </summary>
/// <param name="writeMethod"></param>
private void WriteText(WriteToTextBox writeMethod)
{
string strData = this.txt_Input.Text;
writeMethod(strData);
} /// <summary>
/// 给文本区1赋值
/// </summary>
/// <param name="strTxt"></param>
private void WriteTextBox1(string strTxt)
{
//使用回调
this.txtJobOne.Invoke(writeTxtJobOneCallBack, strTxt);
} /// <summary>
/// 给文本区2赋值
/// </summary>
/// <param name="strTxt"></param>
private void WriteTextBox2(string strTxt)
{
//使用回调
this.txtJobTwo.Invoke(writeTxtJobTwoCallBack, strTxt);
} /// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_Load(object sender, EventArgs e)
{
//设置文本框获取焦点
this.ActiveControl = this.txt_Input; //实例化回调
writeTxtJobOneCallBack = new WriteTxtJobOneCallBack(WriteToTextJobOne);
writeTxtJobTwoCallBack = new WriteTxtJobOneCallBack(WriteToTextJobTwo);
showGroupOneCallBack = new ShowGroupOneCallBack(ShowGroupOne);
showGroupTwoCallBack = new ShowGroupOneCallBack(ShowGroupTwo); } /// <summary>
/// 操作文本区1的回调要执行的方法
/// </summary>
/// <param name="strValue"></param>
private void WriteToTextJobOne(string strValue)
{
this.txtJobOne.Text = strValue;
} /// <summary>
/// 操作文本区2的回调要执行的方法
/// </summary>
/// <param name="strValue"></param>
private void WriteToTextJobTwo(string strValue)
{
this.txtJobTwo.Text = strValue;
} /// <summary>
/// 操作"任务1"分组框的回调要执行的方法
/// </summary>
/// <param name="strValue"></param>
private void ShowGroupOne(string strValue)
{
this.gbJobOne.Text = strValue;
} /// <summary>
/// 操作"任务2"分组框的回调要执行的方法
/// </summary>
/// <param name="strValue"></param>
private void ShowGroupTwo(string strValue)
{
this.gbJobTwo.Text = strValue;
}
}
}

C#网络编程一:C#网络编程常用特性的更多相关文章

  1. vxworks下网络编程一:网络字节序问题

    inet_addr("192.168.1.1");//返回网络字节序整型ip地址inet_ntoa(saddr);//将包含网络字节序整型ip地址的in_addr对象转换成本地ch ...

  2. java网络编程基础——TCP网络编程一

    基于TCP协议的网络编程 TCP/IP协议是一种可靠的网络协议,它的通信的两端各自建立一个Socket,从而在通信的两端之间形成网络虚拟链路. Java使用Socket对象来代表两端的通信端口,并通过 ...

  3. QT 网络编程一

    QT如果要进行网络编程首先需要在.pro中添加如下代码:QT += network 在头文件中包含相关头文件 #include <QHostInfo> #include <QNetw ...

  4. Linux 网络编程一(TCP/IP协议)

    以前我们讲过进程间通信,通过进程间通信可以实现同一台计算机上不同的进程之间通信. 通过网络编程可以实现在网络中的各个计算机之间的通信. 进程能够使用套接字实现和其他进程或者其他计算机通信. 同样的套接 ...

  5. Python学习笔记【第十三篇】:Python网络编程一Socket基础

    什么是⽹络 网络能把双方或多方连在一起的工具,即把数据从一方传递到另一方进行数据传递. 网络编程就是不同电脑上的软件能够进行数据传递.即进程间的通讯. 什么是TCP/IP协议 协议就是大家一起遵守的约 ...

  6. Android初级教程理论知识(第八章网络编程一)

    网络图片查看器 确定图片的网址 发送http请求 URL url = new URL(address); //获取连接对象,并没有建立连接 HttpURLConnection conn = (Http ...

  7. Java网络编程一:基础知识详解

    网络基础知识 1.OSI分层模型和TCP/IP分层模型的对应关系 这里对于7层模型不展开来讲,只选择跟这次系列主题相关的知识点介绍. 2.七层模型与协议的对应关系 网络层   ------------ ...

  8. Linux网络编程一、tcp三次握手,四次挥手

    一.TCP报文格式 (图片来源网络) SYN:请求建立连接标志位 ACK:应答标志位 FIN:断开连接标志位 二.三次握手,数据传输,四次挥手 (流程图,图片来源于网络) (tcp状态转换图,图片来源 ...

  9. Socket网络编程一

    1.Socket参数介绍 A network socket is an endpoint of a connection across a computer network. Today, most ...

随机推荐

  1. ses_cations 值顺序

    16个位置的字符所代表的操作依次如下: 1. ALTER 2. AUDIT 3.COMMENT 4.DELETE 5.GRANT 6.INDEX 7.INSERT 8.LOCK 9.RENAME 10 ...

  2. Android高手速成

    第一部分 个性化控件(View)    http://www.cnblogs.com/huwei0814/p/3796659.html 第二部分 工具库    http://www.cnblogs.c ...

  3. Visual Studio最好用的快捷键

    当然每个人常用的一般都会有些不一样,欢迎大家评论说出自己常用或最常用的快捷键吧,比比看谁用的巧~~~ ctrl+-(shift+ctrl+-):移动光标到上次位置或相反,比如定位一个函数,转到函数定义 ...

  4. LeetCode 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  5. VS 远程调试之 “The visual studio remote debugger does not support this edition of windows”

    The error message "The visual studio remote debugger does not support this edition of windows&q ...

  6. MySQL MEM_ROOT详细讲解

    这篇文章会详细解说MySQL中使用非常广泛的MEM_ROOT的结构体,同时省去debug部分的信息,仅分析正常情况下,mysql中使用MEM_ROOT来做内存分配的部分. 在具体分析之前我们先例举在该 ...

  7. 解决VS2012上面EF字段说明备注没有的方法

    VS2012中的EF有一个BUG 如下: 明明在数据库上面是写有字段说明的到了EF上面就没有了很郁闷: 网络上面有一个解决方法如下: http://www.cnblogs.com/stone_w/ar ...

  8. java socket 通讯

    (转)http://blog.csdn.net/xn4545945/article/details/8098646

  9. 新增资产时YTD折旧与累计折旧录入错误如何处理

    如新增资产时YTD折旧与累计折旧录入错误,但资产已入账处理,如何处理: 1.需要先报废资产: 2.需要在总账手工帐冲销未冲抵凭证: 3.重新增加资产,录入资产时YTD折旧及累计折旧金额应为0.  

  10. 十五、polygon API

    How polygons are handled internally The five basic polygonal API classes Construction History and Tw ...