特性一:委托
委托是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 }