您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關C#網絡編程中常用特性有哪些的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
委托是C#語言中特有的概念,相當于C/C++中的函數指針,與C/C++中函數指針的不同之處是:委托是面向對象的、類型安全的和保險的,是引用類型。因此,對委托的使用要
“先定義、后聲明,接著實例化、然后作為參數傳遞給方法,最后才能使用”。
delegate void SomeDelegate(type1 para1,......typen paran);
SomeDelegate d;
d=new SomeDelegate(obj.InstanceMethod);
其中obj是對象,InstanceMethod是它的實例方法。
someMethod(d);
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(); } } }
多線程的具體介紹請參考博文:http://www.5655pk.com/article/238731.htm
使用多線程實現上一節的程序,代碼如下:
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; } } }
C#回調的具體介紹請參照博文:http://www.5655pk.com/article/238731.htm#_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#網絡編程中常用特性有哪些”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。