1

I have a small application with only a Button and a TextBox. So, when I click button I have to modify text and send some HTTP requests, nothing more. But the text must be modified before any requests are made. I'm doing both operations (text setting and requests) in one thread and all the requests are synchronous HttpWebRequest (because nothing can be done until all requests are finished). This is my click handler code:

messageTb.Text = "Connecting...";
doRequests();

But it works a little bit strange: first, I get all my requests done and only after this the text gets modified.

JustLogin
  • 1,822
  • 5
  • 28
  • 50
  • 1
    That is because you're doing it in the GUI thread. Take a look at this http://stackoverflow.com/questions/8128670/force-redraw-before-long-running-operations – smoksnes Aug 12 '16 at 10:48
  • Try writing `this.Refresh()` after the `messageTb.Text` line. I think that the GUI is not updated before the web request starts, and then because the web request is blocking the UI cannot be updated. You can either force the UI to redraw, or (better) do the request on a background thread – Oliver Aug 12 '16 at 10:50
  • @smoksnes Yes, but text changing is a synchronous operation, so the text must be changed regardless of what's done after. – JustLogin Aug 12 '16 at 10:52
  • 2
    @JustLogin the UI is not required to update immediately when you change the text of one of its controls. – Oliver Aug 12 '16 at 10:52
  • @Oliver I haven't got `Refresh` method in my class (it extends `Window`). – JustLogin Aug 12 '16 at 10:55
  • 1
    If your application is WPF - then this may be a useful link : http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx - it shows how to write a Refresh extension method. (Take note of the 9th comment about WIndows 7). – PaulF Aug 12 '16 at 10:58

0 Answers0