首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

线程里面异步处理有关问题啊 求解决

2013-02-28 
线程里面异步处理问题啊求解决 线程调用Thread th new Thread(new ThreadStart(TRun)) th.Start() pri

线程里面异步处理问题啊 求解决
 线程调用 
 Thread th = new Thread(new ThreadStart(TRun));
 th.Start();

 private void TRun()
 {
   调用委托给控制绑定
   myDelegate1 md1 = new myDelegate1(DataBindUserInfo);
   Invoke(md1);                  
 }

DataBindUserInfo这个方法里面绑定一个树结构(treeview)和一个列表结构(datagrid)
数据量也不是很大   加载时候界面假死啊 有个 5 6秒吧
怎么去显示加载让他不假死啊   解决啊
[解决办法]
多线程操作UI,请使用Delgate 
具体代码参考:


        class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

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

            public void run()
            {
                for (int i = 0; i < 1500; i++)
                {
                    writer wrt = new writer(i.ToString());
                    Thread th = new Thread(new ParameterizedThreadStart(wrt.Startwrite));
                    th.Start(this);
                }
            }

            public void SetRichTextBoxText(string str)
            {
                this.BeginInvoke(new Action(delegate() { this.richTextBox1.Text += str; }));
            }
        }

        class writer
        {
            string aline;
            public writer(string str)
            {
                aline = str;


            }
            public void Startwrite(object form)
            {
                Form1 f = form as Form1;
                f.SetRichTextBoxText(aline);
            }
        }


[解决办法]
那样你可以绑定的时候new一个 treeview 和 datagrid ,绑定完了再设置到画面上去。

热点排行