Why UI freezes during textbox invoking from separated thread
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
}
}
Meanwhile backgroundworker works perfectly - UI doesn't freeze
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
}
}
That's not because of invoke. Your UI queue is full, and it may be because:
DoStuff()
frequentlyUpdate:
According to deleted comment putting 50K of text in text-box was the source of problem. Consider using a smart text box which loads data on demand. There should be one ready out there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With