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


Demo

this.listView1.Visible = true;
            this.listView1.BeginUpdate();this.listView1.EndUpdate();  //结束数据处理,UI界面一次性绘制

 

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using System.Threading;

namespace 子线程操作主线程窗体上的控件
{
    public partial class frmMain : Form
    {
        /***************************************************** 定义该类的私有成员 ****************************************************/
        
        /// <summary>
        /// 定义一个队列,用于记录用户创建的线程
        /// 以便在窗体关闭的时候关闭所有用于创建的线程
        /// </summary>
        private List<Thread> ChaosThreadList;

        /***************************************************** 该类的初始化相关函数 ****************************************************/
                
        /// <summary>
        /// 窗体的初始化函数,初始化线程队列ChaosThreadList
        /// </summary>
        public frmMain()
        {
            InitializeComponent();
            ChaosThreadList = new List<Thread>();
        }
               
        /// <summary>
        /// 窗体的关闭事件处理函数,在该事件中将之前创建的线程全部终止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (ChaosThreadList.Count > 0)
            {
                //编列自定义队列,将所有线程终止
                foreach (Thread tWorkingThread in ChaosThreadList)
                {
                    tWorkingThread.Abort();
                }
            }
        }        

        /***************************************************** 定义该类的自定义函数 ****************************************************/

        /// <summary>
        /// 定义一个代理
        /// </summary>
        /// <param name="index"></param>
        /// <param name="MSG"></param>
        private delegate void DispMSGDelegate(int index,string MSG);

        /// <summary>
        /// 定义一个函数,用于向窗体上的ListView控件添加内容
        /// </summary>
        /// <param name="iIndex"></param>
        /// <param name="strMsg"></param>
        private void DispMsg(int iIndex,string strMsg)
        {
            if (this.lstMain.InvokeRequired==false)                      //如果调用该函数的线程和控件lstMain位于同一个线程内
            {
                //直接将内容添加到窗体的控件上
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems[0].Text = iIndex.ToString();
                lvi.SubItems.Add(strMsg);
                this.lstMain.Items.Insert(0, lvi);
            }
            else                                                        //如果调用该函数的线程和控件lstMain不在同一个线程
            {
                //通过使用Invoke的方法,让子线程告诉窗体线程来完成相应的控件操作
                DispMSGDelegate DMSGD = new DispMSGDelegate(DispMsg);

                //使用控件lstMain的Invoke方法执行DMSGD代理(其类型是DispMSGDelegate)
                this.lstMain.Invoke(DMSGD, iIndex, strMsg);
                
            }
        }

        /// <summary>
        /// 定义一个线程函数,用于循环向列表中添加数据
        /// </summary>
        private void Thread_DisplayMSG()
        {
            for (int i = 0; i < 10000; i++)
            {
                DispMsg(i + 1, "Welcome you : " + (i + 1).ToString());
                Thread.Sleep(10);
            }
        }

        /***************************************************** 定义该类的事件处理函数 ****************************************************/

        /// <summary>
        /// 【开始】按钮的单击事件处理函数,新建一个线程向窗体上的ListView控件填写内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBegin_Click(object sender, EventArgs e)
        {
            //创建一个新的线程
            Thread tWorkingThread = new Thread(Thread_DisplayMSG);

            //将新建的线程加入到自定义线程队列中,以便在窗体结束时关闭所有的线程
            ChaosThreadList.Add(tWorkingThread);

            //开启线程
            tWorkingThread.Start();
        }     

    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM