关于跨线程后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)