5
Warum UI während Textbox friert von getrennten Thread aufruftUI-Thread friert während Textbox Aufruf
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
}
}
Inzwischen Background perfekt funktioniert - UI nicht
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
}
}
Was passiert, wenn Sie 't1.SetApartmentState (ApartmentState.STA)' vor dem Start ausführen? –
Immer noch gleich. Btw UI friert nur für einige hundert Millisekunden ein –
Sie brauchen eigentlich kein synchrones Invoke, für UI können Sie nur BeginInvoke aufrufen. Versuchen Sie außerdem, einen kleineren Text festzulegen. Vielleicht ist es nur ein bisschen groß für einen Standard TextBox –