C#网络编程一:C#网络编程常用特性
Posted .NET开发菜鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#网络编程一:C#网络编程常用特性相关的知识,希望对你有一定的参考价值。
特性一:委托
委托是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”复选框后单击“提交”按钮,程序会自动将文本框中的文字“抄写”到对应的用户勾选的文本区中去。
代码实现如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace DelegateDemo 12 { 13 public partial class FrmMain : Form 14 { 15 public FrmMain() 16 { 17 InitializeComponent(); 18 } 19 20 //1、定义委托 21 private delegate void WriteToTextBox(string strTxt); 22 //2、声明委托 23 private WriteToTextBox writeToTextBox; 24 25 /// <summary> 26 /// 提交 27 /// </summary> 28 /// <param name="sender"></param> 29 /// <param name="e"></param> 30 private void btn_OK_Click(object sender, EventArgs e) 31 { 32 if (chbOne.Checked) 33 { 34 gbJobOne.Text = "运行中......"; 35 gbJobOne.Refresh(); 36 txtJobOne.Clear(); 37 //3、实例化委托 38 writeToTextBox = new WriteToTextBox(WriteTextBox1); 39 //4、将委托作为方法的参数进行传递 40 WriteText(writeToTextBox); 41 gbJobOne.Text = "任务1完成"; 42 } 43 if (chbTwo.Checked) 44 { 45 46 gbJobTwo.Text = "运行中......"; 47 gbJobTwo.Refresh(); 48 txtJobTwo.Clear(); 49 //3、实例化委托 50 writeToTextBox = new WriteToTextBox(WriteTextBox2); 51 //4、将委托作为方法的参数进行传递 52 WriteText(writeToTextBox); 53 gbJobTwo.Text = "任务2完成"; 54 } 55 } 56 57 58 private void WriteText(WriteToTextBox writeMethod) 59 { 60 string strData = this.txt_Input.Text; 61 writeMethod(strData); 62 } 63 private void WriteTextBox1(string strTxt) 64 { 65 this.txtJobOne.Text = strTxt; 66 } 67 68 private void WriteTextBox2(string strTxt) 69 { 70 this.txtJobTwo.Text = strTxt; 71 } 72 73 /// <summary> 74 /// 窗体加载事件 75 /// </summary> 76 /// <param name="sender"></param> 77 /// <param name="e"></param> 78 private void FrmMain_Load(object sender, EventArgs e) 79 { 80 //设置文本框获取焦点 81 this.ActiveControl = this.txt_Input; 82 //this.txt_Input.Focus(); 83 } 84 } 85 }
特性2:多线程
多线程的具体介绍请参考博文:http://www.cnblogs.com/dotnet261010/p/6159984.html
使用多线程实现上一节的程序,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Threading;//引入多线程的命名空间 11 12 namespace DelegateDemo 13 { 14 public partial class FrmMain : Form 15 { 16 public FrmMain() 17 { 18 InitializeComponent(); 19 } 20 21 //1、定义委托 22 private delegate void WriteToTextBox(string strTxt); 23 //2、声明委托 24 private WriteToTextBox writeToTextBox; 25 26 /// <summary> 27 /// 提交 28 /// </summary> 29 /// <param name="sender"></param> 30 /// <param name="e"></param> 31 private void btn_OK_Click(object sender, EventArgs e) 32 { 33 //创建线程1 34 Thread thread1 = new Thread(new ThreadStart(ExecuteTsk1)); 35 //启动线程1 36 thread1.Start(); 37 38 //创建线程2 39 Thread thread2 = new Thread(new ThreadStart(ExecuteTsk2)); 40 //启动线程2 41 thread2.Start(); 42 43 } 44 45 46 private void ExecuteTsk1() 47 { 48 if (chbOne.Checked) 49 { 50 gbJobOne.Text = "运行中......"; 51 gbJobOne.Refresh(); 52 txtJobOne.Clear(); 53 //3、实例化委托 54 writeToTextBox = new WriteToTextBox(WriteTextBox1); 55 //4、将委托作为方法的参数进行传递 56 WriteText(writeToTextBox); 57 gbJobOne.Text = "任务1完成"; 58 } 59 } 60 61 private void ExecuteTsk2() 62 { 63 if (chbTwo.Checked) 64 { 65 66 gbJobTwo.Text = "运行中......"; 67 gbJobTwo.Refresh(); 68 txtJobTwo.Clear(); 69 //3、实例化委托 70 writeToTextBox = new WriteToTextBox(WriteTextBox2); 71 //4、将委托作为方法的参数进行传递 72 WriteText(writeToTextBox); 73 gbJobTwo.Text = "任务2完成"; 74 } 75 } 76 77 78 private void WriteText(WriteToTextBox writeMethod) 79 { 80 string strData = this.txt_Input.Text; 81 writeMethod(strData); 82 } 83 private void WriteTextBox1(string strTxt) 84 { 85 this.txtJobOne.Text = strTxt; 86 } 87 88 private void WriteTextBox2(string strTxt) 89 { 90 this.txtJobTwo.Text = strTxt; 91 } 92 93 /// <summary> 94 /// 窗体加载事件 95 /// </summary> 96 /// <param name="sender"></param> 97 /// <param name="e"></param> 98 private void FrmMain_Load(object sender, EventArgs e) 99 { 100 //设置文本框获取焦点 101 this.ActiveControl = this.txt_Input; 102 //允许跨线程调用 103 Control.CheckForIllegalCrossThreadCalls = false; 104 } 105 } 106 }
特性3:C#方法回调
C#回调的具体介绍请参照博文:http://www.cnblogs.com/dotnet261010/p/6159984.html
使用委托、多线程和C#的方法回调机制实现上一节的程序,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Threading;//引入多线程的命名空间 11 12 namespace DelegateDemo 13 { 14 public partial class FrmMain : Form 15 { 16 public FrmMain() 17 { 18 InitializeComponent(); 19 } 20 21 //1、定义委托 22 private delegate void WriteToTextBox(string strTxt); 23 //2、声明委托 24 private WriteToTextBox writeToTextBox; 25 26 //定义并声明操作文本区1的回调 27 private delegate void WriteTxtJobOneCallBack(string strValue); 28 WriteTxtJobOneCallBack writeTxtJobOneCallBack; 29 30 //定义并声明操作文本区2的回调 31 private delegate void WriteTxtJobTwoCallBack(string strValue); 32 WriteTxtJobOneCallBack writeTxtJobTwoCallBack; 33 34 //定义并声明操作"任务1"分组框的回调 35 private delegate void ShowGroupOneCallBack(string strValue); 36 ShowGroupOneCallBack showGroupOneCallBack; 37 38 //定义并声明操作"任务2"分组框的回调 39 private delegate void ShowGroupTwoCallBack(string strValue); 40 ShowGroupOneCallBack showGroupTwoCallBack; 41 42 43 44 /// <summary> 45 /// 提交 46 /// </summary> 47 /// <param name="sender"></param> 48 /// <param name="e"></param> 49 private void btn_OK_Click(object sender, EventArgs e) 50 { 51 //创建线程1 52 Thread thread1 = new Thread(new ThreadStart(ExecuteTsk1)); 53 //启动线程1 54 thread1.Start(); 55 56 //创建线程2 57 Thread thread2 = new Thread(new ThreadStart(ExecuteTsk2)); 58 //启动线程2 59 thread2.Start(); 60 61 } 62 63 64 private void ExecuteTsk1() 65 { 66 if (chbOne.Checked) 67 { 68 //3、实例化委托 69 writeToTextBox = new WriteToTextBox(WriteTextBox1); 70 //4、将委托作为方法的参数进行传递 71 WriteText(writeToTextBox); 72 //使用回调 73 this.gbJobOne.Invoke(showGroupOneCallBack, "任务1"); 74 } 75 } 76 77 private void ExecuteTsk2() 78 { 79 if (chbTwo.Checked) 80 { 81 //3、实例化委托 82 writeToTextBox = new WriteToTextBox(WriteTextBox2); 83 //4、将委托作为方法的参数进行传递 84 WriteText(writeToTextBox); 85 //使用回调 86 this.gbJobTwo.Invoke(showGroupTwoCallBack, "任务2"); 87 } 88 } 89 90 /// <summary> 91 /// 执行自定义委托 92 /// </summary> 93 /// <param name="writeMethod"></param> 94 private void WriteText(WriteToTextBox writeMethod) 95 { 96 string strData = this.txt_Input.Text; 97 writeMethod(strData); 98 } 99 100 /// <summary> 101 /// 给文本区1赋值 102 /// </summary> 103 /// <param name="strTxt"></param> 104 private void WriteTextBox1(string strTxt) 105 { 106 //使用回调 107 this.txtJobOne.Invoke(writeTxtJobOneCallBack, strTxt); 108 } 109 110 /// <summary> 111 /// 给文本区2赋值 112 /// </summary> 113 /// <param name="strTxt"></param> 114 private void WriteTextBox2(string strTxt) 115 { 116 //使用回调 117 this.txtJobTwo.Invoke(writeTxtJobTwoCallBack, strTxt); 118 } 119 120 /// <summary> 121 /// 窗体加载事件 122 /// </summary> 123 /// <param name="sender"></param> 124 /// <param name="e"></param> 125 private void FrmMain_Load(object sender, EventArgs e) 126 { 127 //设置文本框获取焦点 128 this.ActiveControl = this.txt_Input; 129 130 //实例化回调 131 writeTxtJobOneCallBack = new WriteTxtJobOneCallBack(WriteToTextJobOne); 132 writeTxtJobTwoCallBack = new WriteTxtJobOneCallBack(WriteToTextJobTwo); 133 showGroupOneCallBack = new ShowGroupOneCallBack(ShowGroupOne); 134 showGroupTwoCallBack = new ShowGroupOneCallBack(ShowGroupTwo); 135 136 } 137 138 /// <summary> 139 /// 操作文本区1的回调要执行的方法 140 /// </summary> 141 /// <param name="strValue"></param> 142 private void WriteToTextJobOne(string strValue) 143 { 144 this.txtJobOne.Text = strValue; 145 } 146 147 /// <summary> 148 /// 操作文本区2的回调要执行的方法 149 /// </summary> 150 /// <param name="strValue"></param> 151 private void WriteToTextJobTwo(string strValue) 152 { 153 this.txtJobTwo.Text = strValue; 154 } 155 156 /// <summary> 157 /// 操作"任务1"分组框的回调要执行的方法 158 /// </summary> 159 /// <param name="strValue"></param> 160 private void ShowGroupOne(string strValue) 161 { 162 this.gbJobOne.Text = strValue; 163 } 164 165 /// <summary> 166 /// 操作"任务2"分组框的回调要执行的方法 167 /// </summary> 168 /// <param name="strValue"></param> 169 private void ShowGroupTwo(string strValue) 170 { 171 this.gbJobTwo.Text = strValue; 172 } 173 } 174 }
以上是关于C#网络编程一:C#网络编程常用特性的主要内容,如果未能解决你的问题,请参考以下文章