我的故事

海韵
网站日历
73 2018 - 12 48
      1
2345678
9101112131415
16171819202122
23242526272829
3031

栏目导航
    全部文章
    随笔杂谈
    文学欣赏
    休闲娱乐
    热点综述
    信息技术
最新评论
统计信息
日记搜索

本站搜索百度搜索
其他信息
BLOG编码
RSS 1.0
RSS 2.0
未知 [信息技术] C#2.0多线程跨线程访问UI    阅读:12168    

C#2.0多线程跨线程访问UI
作者:haiyun 编辑:海韵 发布日期:2007-03-12 点击量:12168 来源: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();
        }
    }


}


[ haiyun 于 2007年3月12日 0:00:00 周一  ]  

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


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