未知 [信息技术] C#2.0多线程跨线程访问UI     阅读:18177  

C#2.0多线程跨线程访问UI
作者: haiyun 编辑: 海韵 发布日期: 2007-03-12 00:00:00 点击量: 18177 来源: haiyun.net

C#2.0多线程跨线程访问UI

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

namespace TestTheads
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ArrayList threads = new ArrayList();
        public delegate void ChangeText(int i,int j); //定义一个委托来改变变化的数字
        public delegate void setText(int ct);//定义委托来改变文字

        private void button1_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void Add()
        {
            int count = threads.Count;
            if (count < 10)
            {

                //使用委托来改变控件的显示
                this.Invoke(new setText(this.Settexts), new object[] { count });
                Thread t = new Thread(new ThreadStart(Process));
                t.Start();
                threads.Add(t);
                
            }
        }

        private void Del()
        {
            int count = threads.Count;
            if (count > 0)
            {
                Thread t1 = (Thread)threads[count - 1];
                if (t1.IsAlive)
                {
                    t1.Abort();
                }
                threads.RemoveAt(count - 1);
                lock (listView1)
                {
                    listView1.Items.RemoveAt(count - 1);
                }
            }
        }


        private void Process()
        {
                //CheckForIllegalCrossThreadCalls = false;//如果使用这句话则可以不使用委托而使用非安全的方法来访问控件
                int i = 1;
                while (true)
                {
                    i++;
                    int j = threads.IndexOf(Thread.CurrentThread);
                      //利用委托来执行
                      this.Invoke(new ChangeText(this.Display), new object[] { j,i });

                    Thread.Sleep(10);
                }
 
        }

        //动态更改LISTVIEW
        private void Display(int j,int i)
        {
            lock (listView1)
            {
                this.listView1.Items[j].SubItems[1].Text = i.ToString();
            }
        }

        //设置LISTVIEW的值
        private void Settexts(int cts)
        {
            lock (listView1)
            {
                listView1.Items.Insert(cts, new ListViewItem(new string[] { cts.ToString(), "0" }));
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            Del();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            disposethead();
        }

        private void disposethead()
        {
            while (threads.Count > 0)
            {
                Thread t1 = (Thread)threads[0];
                if (t1.IsAlive)
                {
                    t1.Abort();
                }
                threads.RemoveAt(0);
                lock (listView1)
                {
                    listView1.Items.RemoveAt(0);
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            disposethead();
        }
    }


}


[ 海韵 于 2007-03-12 00:00:00  Monday   ]  
相关文章:

网友评论
正在加载评论...


免责申明 | 版权申明 | 联系我们 | 关于
Copyright © 2001-2024 Haiyun.Net All Rights Reserved
地址:深圳市南山区南油天安工业区1栋3楼 邮编:518000
有我可以帮您的吗?QQ:12457275  
粤ICP备05017321号-1