C#多线程编程实例-线程与窗体交互的代码分析

黄舟
Lepaskan: 2017-03-13 11:37:07
asal
2872 orang telah melayarinya

C#多线程编程实例 线程与窗体交互

代码:

    public partial class Form1 : Form
    {
        //声明线程数组
        Thread[] workThreads = new Thread[10];

        public Form1()
        {
            InitializeComponent();
        }

        //此委托允许异步的调用为Listbox添加Item
        delegate void AddItemCallback(string text);

        //这种方法演示如何在线程安全的模式下调用Windows窗体上的控件。
        private void AddItem(string text)
        {
            if (this.listBox1.InvokeRequired)
            {
                AddItemCallback d = new AddItemCallback(AddItem);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.listBox1.Items.Add(text);
            }
        }

        //数据采集方法
        public void DataGet()
        {
            while (true)
            {
                AddItem("ok");
                Thread.Sleep(1000);
            }
            //如果此方法退出了,那么这个线程也就退出了
        }

        /// <summary>
        /// 启动线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //循环创建并启动线程执行
            for (int i = 0; i < workThreads.Length; i++)
            {
                if (workThreads[i] == null)
                {
                    //如果线程不存在,则创建
                    workThreads[i] = new Thread(new ThreadStart(DataGet));
                    workThreads[i].Name = i.ToString();
                    workThreads[i].Start();
                }
                else
                {
                    //已经存在,如果没有运行,则启动
                    if (workThreads[i].ThreadState == ThreadState.Aborted || workThreads[i].ThreadState == ThreadState.Stopped)
                    {
                        workThreads[i] = new Thread(new ThreadStart(DataGet));
                        workThreads[i].Name = i.ToString();
                        workThreads[i].Start();
                    }else{
                        workThreads[i].Start();
                    }
                }
            }
        }

        /// <summary>
        /// 停止线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //循环停止线程执行
            for (int i = 0; i < workThreads.Length; i++)
            {
                //如果线程存在,并且状态不是停止也不是终止的,则终止该线程
                if (workThreads[i] != null && workThreads[i].ThreadState != ThreadState.Stopped && workThreads[i].ThreadState != ThreadState.Aborted)
                {
                    workThreads[i].Abort();
                }
            }
        }
Salin selepas log masuk

Atas ialah kandungan terperinci C#多线程编程实例-线程与窗体交互的代码分析. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan