0

In my program, I append text to a textbox and my program proceeds onward before the textbox finishes rendering. This is bad karma to the user.

To demonstrate this issue, I have written to the System.Console and I see many text lines displayed in the MS Visual C# Express console tab before the text is displayed in the control (in my application).

I have tried Textbox.Update() with no luck, program keeps executing. Textbox.Refresh() does not help either.

How do I get a thread (the GUI or another thread) to wait until the Textbox has finished rendering?

I want to perform the following (in psuedo code):

Textbox.AppendText("New text\r\n");

// Execution blocked until Textbox finishes drawing.
Textbox.WaitForRenderingToFinish();
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 1
    You could try Application.DoEvents(). This method temporarily passes control to the message pump allowing UI events to be processed. More information about why the text box is taking so long to render and why you need to wait would be useful. – Mike Mar 17 '11 at 22:18
  • Maybe wait for one of the right events to fire? http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events.aspx – Mike Atlas Mar 17 '11 at 22:21
  • @Mike: I am displaying status in the textbox, such as "initializing communications with external computer". I would like the text displayed before the initialization with the external computer starts. Kind of giving the User truth in advertising. – Thomas Matthews Mar 17 '11 at 22:39
  • The Application.DoEvents() has no effect. There is still a noticeable delay from when the text is given to the Textbox and when it is actually rendered. Many console messages still go by. – Thomas Matthews Mar 17 '11 at 22:43
  • @Mike Atlas: Which `event` do you recommend? The `TextChanged` event could occur when the `Textbox` has received new text or it could fire when the `Textbox` has finished processing text. My understanding is that it would fire each time a character was changed, which is not what I want. – Thomas Matthews Mar 17 '11 at 22:52

1 Answers1

0

What happens if you do

Textbox.AppendText("New text\r\n");
Application.DoEvents();

As discussed in this previous post: How to force Buttons, TextBoxes to repaint on form after a MessageBox closes in C# it is not good practice to do work on the UI thread. Is this what you are doing? Is the entire UI unresponsive as you are proceeding with the program execution, or is it just the textbox that won't update?

Community
  • 1
  • 1
Simen S
  • 3,210
  • 18
  • 24
  • : No noticeable improvement from adding `Application.DoEvents()`. – Thomas Matthews Mar 17 '11 at 22:43
  • Here is another thread I just found which may be related: http://stackoverflow.com/questions/439974/how-to-force-buttons-textboxes-to-repaint-on-form-after-a-messagebox-closes-in-c – Simen S Mar 17 '11 at 23:01