| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1431 人关注过本帖
标题:关于跨线程后UI一样假死问题
只看楼主 加入收藏
zhutoudd
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-6-24
收藏
 问题点数:0 回复次数:4 
关于跨线程后UI一样假死问题
做了一个小程序,先提示了不能跨线程操作控件,后来用了
            this.Invoke(new EventHandler(delegate
{
}));
后,运行一样假死,不知道为什么。希望高手能帮帮忙啦,本人也刚学不久,谢了。


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

        private void Search()
        {
            
            this.Invoke(new EventHandler(delegate
            {
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
            }));
        }

        private void SearchPaht(string path)
        {

            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] f = di.GetFiles("*.*");
            listBox1.BeginUpdate();
            foreach (FileInfo file in f)
            {
                listBox1.Items.Add(file.FullName);
            }
            listBox1.EndUpdate();
            DirectoryInfo[] d = di.GetDirectories();
            foreach (DirectoryInfo dir in d)
            {
                listBox1.Items.Add(dir.FullName);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(Search));
            t.Start();
        }
    }
}

WindowsApplication11.rar (31.54 KB)
搜索更多相关主题的帖子: 线程 假死 
2008-10-19 01:22
zhutoudd
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-6-24
收藏
得分:0 
附件就是上面那些代码,如果看的乱就麻烦下载下来看一眼吧,小弟现谢过了。
2008-10-19 01:23
ecjob
Rank: 3Rank: 3
等 级:论坛游侠
威 望:8
帖 子:321
专家分:110
注 册:2006-10-15
收藏
得分:0 
在循环中加个 Application.DoEvents();
        private void Search()
        {
            
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
        }

        private void SearchPaht(string path)
        {

            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] f = di.GetFiles("*.*");
            listBox1.BeginUpdate();
            foreach (FileInfo file in f)
            {
                listBox1.Items.Add(file.FullName);
            }
            listBox1.EndUpdate();
            DirectoryInfo[] d = di.GetDirectories();
            foreach (DirectoryInfo dir in d)
            {
                Application.DoEvents();
                listBox1.Items.Add(dir.FullName);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Search();
        }

86年出生,广东求 女人/项目/工作 ,见广告即有效.论坛PM
2008-10-19 01:29
ecjob
Rank: 3Rank: 3
等 级:论坛游侠
威 望:8
帖 子:321
专家分:110
注 册:2006-10-15
收藏
得分:0 
用 Application.DoEvents();速度会有影响。 可以试下直接加入ITEMS


        private void Search()
        {
            
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
                SearchPaht(@"C:\WINDOWS\system32");
        }

        private void SearchPaht(string path)
        {

            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] f = di.GetFiles("*.*");
            Array o_arr;
            o_arr.
            List <string> o_str路径数组=new List<string> ();
            foreach (FileInfo file in f)
            {
                Application.DoEvents();
                o_str路径数组.Add(file.FullName);
            }
            listBox1.Items.AddRange(o_str路径数组.ToArray ());
            o_str路径数组.Clear();
            DirectoryInfo[] d = di.GetDirectories();
            foreach (DirectoryInfo dir in d)
            {
                Application.DoEvents();
                o_str路径数组.Add(dir.FullName);
            }
            listBox1.Items.AddRange(o_str路径数组.ToArray());
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Search();
        }

86年出生,广东求 女人/项目/工作 ,见广告即有效.论坛PM
2008-10-19 01:39
zhutoudd
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-6-24
收藏
得分:0 
果然高手,解决了,小弟谢过!
2008-10-19 10:23
快速回复:关于跨线程后UI一样假死问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038880 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved