-1

Here is my simple codes :

   private async void button1_Click(object sender, EventArgs e)
    {
      TextBox1.Text = "";
      await MyMethodAsync();
    }

    public async Task MyMethodAsync()
    {
      TextBox1.Text = "Test 1" + Environment.NewLine;  

      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(main_url + "Appointment/LoadVisaAppointmentTypeDetail?id=");
      req.Method = "GET";
      req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
      req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
      req.ContentType = "text/html; charset=utf-8";
      req.KeepAlive = true;
      req.Timeout = 25000;
      req.AllowAutoRedirect = true;
      res = (HttpWebResponse)req.GetResponse();
      Stream Stream = res.GetResponseStream();
      StreamReader reader = new StreamReader(Stream);
      string reader_str = reader.ReadToEnd();
      ...
      TextBox1.Text += "Test 2" + Environment.NewLine;
      ...
      TextBox1.Text += "Done";
    }

During running these codes i can not move scroll bar of TextBox1 & i can see the log after the Done line.
How can i unblock TextBox1 during async Task MyMethodAsync() working?

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • Did you try to put a simple "DoEvents" to force UI update? – David BS Jun 10 '17 at 01:11
  • Show us more complete code with the `...` parts. – Idle_Mind Jun 10 '17 at 01:14
  • Move the content of `MyMethodAsync` into the event handler so that the `// "lot of IO and stuff"` is awaited without block UI thread and the update of `TextBox1.Text` happens on the UI thread – Nkosi Jun 10 '17 at 01:28
  • if you're not on the UI thread it will throw an exception, you'll have to marshal it back to the ui – Noctis Jun 10 '17 at 01:31
  • 1
    There's nothing in the code you posted that would cause your long-running operation to run asynchronously. Assuming you didn't turn warnings off, you would even get a message from the compiler to that effect. Pay attention to what the compiler tells you; it's important. If you think you are running the code asynchronously, you need to post a good [mcve] that actually demonstrates that. – Peter Duniho Jun 10 '17 at 23:25
  • While the linked duplicate was a fine answer in 2009, today you should certainly prefer `Task.Run` over `StartNew` / `QueueUserWorkItem` / `BackgroundWorker`. – Stephen Cleary Jun 12 '17 at 14:10

1 Answers1

3

You could try something like this:

private async void button1_Click(object sender, EventArgs e)
{
    string reader_str = "";

    textBox1.Text = "Test 1" + Environment.NewLine;          
    await Task.Run(() =>
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(main_url + "Appointment/LoadVisaAppointmentTypeDetail?id=");
        req.Method = "GET";
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
        req.ContentType = "text/html; charset=utf-8";
        req.KeepAlive = true;
        req.Timeout = 25000;
        req.AllowAutoRedirect = true;
        res = (HttpWebResponse)req.GetResponse();
        Stream Stream = res.GetResponseStream();
        StreamReader reader = new StreamReader(Stream);
        reader_str = reader.ReadToEnd();
    });
    textBox1.AppendText(reader_str + Environment.NewLine + Environment.NewLine);

    textBox1.AppendText("Test 2" + Environment.NewLine);
    await Task.Run(() =>
    {
        // ...
    });
    textBox1.AppendText(reader_str + Environment.NewLine + Environment.NewLine);
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40