特性一:委托
委托是C#语言中特有的概念,相当于C/C++中的函数指针,与C/C++中函数指针的不同之处是:委托是面向对象的、类型安全的和保险的,是引用类型。因此,对委托的使用要
“先定义、后声明,接着实例化、然后作为参数传递给方法,最后才能使用”。
1、定义委托使用关键字delegate:
delegate void SomeDelegate(type1 para1,......typen paran);
2、声明委托:
SomeDelegate d;
3、实例化委托:
1
|
d= new SomeDelegate(obj.InstanceMethod); |
其中obj是对象,InstanceMethod是它的实例方法。
4、作为参数传递给方法
1
|
someMethod(d); |
5、最后在此方法的实现代码中使用委托
1
2
3
4
5
6
7
|
private void someMethod(SomeDelegate someDelegate) { ..... //使用委托 someDelegate(arg1,arg2,....,argn); ...... } |
通过委托SomeDelegate实现对方法InstanceMethod的调用,调用还必须有一个前提条件:方法InstanceMethod有参数且和定义SomeDelegate的参数一致,并且返回类型相同(本例中为void)。方法InstanceMethod的定义:
1
2
3
4
5
|
private void InstanceMethod(type1 para1,type2 para2,......,typen paran) { //方法体 ..... } |
委托的实例化中的参数既可以是实例方法,也可以是静态方法。
使用委托实现“文字抄写员”的小程序,界面如下:
在下方文本框中编辑文字,勾选“书写到”组框中的“文本区1”和(或)“文本区2”复选框后单击“提交”按钮,程序会自动将文本框中的文字“抄写”到对应的用户勾选的文本区中去。
代码实现如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
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.tuohang.net/article/263015.html
使用多线程实现上一节的程序,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
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.tuohang.net/article/263015.html
使用委托、多线程和C#的方法回调机制实现上一节的程序,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
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#网络编程中常用特性的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/dotnet261010/p/6206068.html