Replacing a UI Thread During a Text Field Call

Why does the user interface freeze while calling a text field from a dedicated 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, the background worker is working fine - the user interface does not 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
        }
    }
+5
source share
1 answer

This is not due to a call. Your user interface queue is full, and maybe because:

  • You often call DoStuff()
  • You perform other difficult tasks in the user interface

Update:

50% . , . .

+1