日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中子线程操作主线程中窗体上控件的方法

發(fā)布時(shí)間:2024/10/12 C# 113 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中子线程操作主线程中窗体上控件的方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Demo

this.listView1.Visible = true;this.listView1.BeginUpdate();this.listView1.EndUpdate(); //結(jié)束數(shù)據(jù)處理,UI界面一次性繪制

?

using System; using System.Collections.Generic; using System.Windows.Forms;using System.Threading;namespace 子線程操作主線程窗體上的控件 {public partial class frmMain : Form{/***************************************************** 定義該類的私有成員 ****************************************************//// <summary>/// 定義一個(gè)隊(duì)列,用于記錄用戶創(chuàng)建的線程/// 以便在窗體關(guān)閉的時(shí)候關(guān)閉所有用于創(chuàng)建的線程/// </summary>private List<Thread> ChaosThreadList;/***************************************************** 該類的初始化相關(guān)函數(shù) ****************************************************//// <summary>/// 窗體的初始化函數(shù),初始化線程隊(duì)列ChaosThreadList/// </summary>public frmMain(){InitializeComponent();ChaosThreadList = new List<Thread>();}/// <summary>/// 窗體的關(guān)閉事件處理函數(shù),在該事件中將之前創(chuàng)建的線程全部終止/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void frmMain_FormClosed(object sender, FormClosedEventArgs e){if (ChaosThreadList.Count > 0){//編列自定義隊(duì)列,將所有線程終止foreach (Thread tWorkingThread in ChaosThreadList){tWorkingThread.Abort();}}} /***************************************************** 定義該類的自定義函數(shù) ****************************************************//// <summary>/// 定義一個(gè)代理/// </summary>/// <param name="index"></param>/// <param name="MSG"></param>private delegate void DispMSGDelegate(int index,string MSG);/// <summary>/// 定義一個(gè)函數(shù),用于向窗體上的ListView控件添加內(nèi)容/// </summary>/// <param name="iIndex"></param>/// <param name="strMsg"></param>private void DispMsg(int iIndex,string strMsg){if (this.lstMain.InvokeRequired==false) //如果調(diào)用該函數(shù)的線程和控件lstMain位于同一個(gè)線程內(nèi) {//直接將內(nèi)容添加到窗體的控件上ListViewItem lvi = new ListViewItem();lvi.SubItems[0].Text = iIndex.ToString();lvi.SubItems.Add(strMsg);this.lstMain.Items.Insert(0, lvi);}else //如果調(diào)用該函數(shù)的線程和控件lstMain不在同一個(gè)線程 {//通過使用Invoke的方法,讓子線程告訴窗體線程來完成相應(yīng)的控件操作DispMSGDelegate DMSGD = new DispMSGDelegate(DispMsg);//使用控件lstMain的Invoke方法執(zhí)行DMSGD代理(其類型是DispMSGDelegate)this.lstMain.Invoke(DMSGD, iIndex, strMsg);}}/// <summary>/// 定義一個(gè)線程函數(shù),用于循環(huán)向列表中添加數(shù)據(jù)/// </summary>private void Thread_DisplayMSG(){for (int i = 0; i < 10000; i++){DispMsg(i + 1, "Welcome you : " + (i + 1).ToString());Thread.Sleep(10);}}/***************************************************** 定義該類的事件處理函數(shù) ****************************************************//// <summary>/// 【開始】按鈕的單擊事件處理函數(shù),新建一個(gè)線程向窗體上的ListView控件填寫內(nèi)容/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnBegin_Click(object sender, EventArgs e){//創(chuàng)建一個(gè)新的線程Thread tWorkingThread = new Thread(Thread_DisplayMSG);//將新建的線程加入到自定義線程隊(duì)列中,以便在窗體結(jié)束時(shí)關(guān)閉所有的線程 ChaosThreadList.Add(tWorkingThread);//開啟線程 tWorkingThread.Start();} } }

?

轉(zhuǎn)載于:https://www.cnblogs.com/test404/p/6715118.html

總結(jié)

以上是生活随笔為你收集整理的C#中子线程操作主线程中窗体上控件的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。