首頁 > 軟體

C#網路程式設計中常用特性介紹

2022-02-24 22:00:23

特性一:委託

委託是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://www.jb51.net/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;
        }
    }
}

特性3:C#方法回撥

C#回撥的具體介紹請參照博文:https://www.jb51.net/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#網路程式設計中常用特性的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com