C#网络编程常用特性
.NET开发菜鸟 人气:0特性一:委托
委托是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:多线程
多线程的具体介绍请参考博文:https:
使用多线程实现上一节的程序,代码如下:
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#回调的具体介绍请参照博文:https:#_label3
使用委托、多线程和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#网络编程中常用特性的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。
加载全部内容