为什么在从单独线程调用文本框期间 UI 冻结
private void button1_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(DoStuff);
t1.Start();
}
void DoStuff()
{
using (var wc = new System.Net.WebClient())
{
string page_src = wc.DownloadString("http://bing.com");
textBox1.Invoke((MethodInvoker)delegate() { textBox1.Text = page_src; }); // freezes while textbox text is changing
}
}
同时 backgroundworker 完美运行 - UI 不会冻结
private void button1_Click(object sender, EventArgs e)
{
BackgroundWorker bw1 = new BackgroundWorker();
bw1.DoWork += (a, b) => { DoStuff(); };
bw1.RunWorkerAsync();
}
void DoStuff()
{
using (var wc = new System.Net.WebClient())
{
string res = wc.DownloadString("http://bing.com");
textBox1.Invoke((MethodInvoker)delegate() { textBox1.Text = res; }); // works great
}
}