Write to textbox

P

pincro

I am trying to append text to a textbox while in a loop.
My problem is that the text is written only after the loop completes.
How can I write to the textbox while in the loop?

Here is a sample...
for (int a = 0; a < 500; a++)
{
textBox1.Text += a.ToString() + Environment.NewLine;
}
 
S

Stanimir Stoyanov

The text is actually appended to the text box but because the UI refresh is
performed on the same thread, the loop is blocking it from refreshing. To
force the refresh, place a call to Application.DoEvents(); in the end of the
loop body (before the closing }).
 
P

pincro

Thanks. I knew it was something simple.



Stanimir Stoyanov said:
The text is actually appended to the text box but because the UI refresh is
performed on the same thread, the loop is blocking it from refreshing. To
force the refresh, place a call to Application.DoEvents(); in the end of the
loop body (before the closing }).
 
I

Ignacio Machin ( .NET/ C# MVP )

I am trying to append text to a textbox while in a loop.
My problem is that the text is written only after the loop completes.
How can I write to the textbox while in the loop?

Here is a sample...
for (int a = 0; a < 500; a++)
{
textBox1.Text += a.ToString() + Environment.NewLine;
}

Are you sure you want this?
the screen will flash like crazy , heck most probably you will not
even see it as a loop of 500 iterations is performed in no time at
all.

What is what you want in the first place?
 
N

Nicholas Paldino [.NET/C# MVP]

That's actually a REALLY bad idea. Generally speaking, you shouldn't be
calling DoEvents in your code. It's nothing more than a hack in most cases
(there are very few legitimate uses for DoEvents, and this is not one of
them).

What you want to do is run this loop on another thread and then have it
call Invoke on the TextBox, passing a delegate which will actually update
the text.
 
S

Stanimir Stoyanov

Nicholas, Peter:

I stand corrected. BackgroundWorker would be a better choice. :)
 
P

pincro

This loop is a "proof of concept". I thought this would be the simplest way
to test and demonstrate my problem to you.

The Application.DoEvents() does work. I will also look into using threads.
I just want to do it right the first time.
 
I

Ignacio Machin ( .NET/ C# MVP )

This loop is a "proof of concept". I thought this would be the simplest way
to test and demonstrate my problem to you.

Of what problem?
The Application.DoEvents() does work. I will also look into using threads.
I just want to do it right the first time.

As other mentioned in this thread calling DoEvents is not a very good
idea. Why don't ou better explain what is your problem and we can
suggest an appropriated solution
 
B

Bob

NO! Do _not_ do this.

If you've got a loop like that blocking the main GUI thread, the correct
solution is to put the loop into a different thread.

Using Application.DoEvents() and related methods is almost always a hack
to get around a design problem. It introduces re-entrancy into code that
was almost certainly not designed for re-entrancy, and opens the door for
bugs.

Doing the work in a different thread is not difficult at all (see
BackgroundWorker or ThreadPool, and Control.Invoke()), and results in much
better code.

Pete

I've run into cases where the UI thread got starved for cycles when a
similar background thread was running (BackgroundWorker, generating
items for updating the control contents). Calls to controls were
marshaled via delegate/Invoke, of course. Same end result as the OP
here: no control updates until after the thread was finished.

I ended up just putting the thread to sleep every once in a while,
which works, but seems like a hack as well. There must be a better way
to force the UI thread to run more often.
 
B

Bob

It will run as often as it needs to, assuming you haven't interfered with
it somehow.

If you put a long-lived task on a background thread and still had trouble
with the UI not being updated often enough, there must have been something
wrong with the implementation. Of course, without seeing a
concise-but-complete code example that demonstrates the problem, it's not
possible to say _what_ was wrong with it. But suffice to say, correct
code doesn't behave in the way you describe.

Pete

Well, I was surprised. The thread was doing file access and going to
SQL server, so it was more complex than the OP's example. Too much
code to factor out samples, but basically, the thread simply seemed to
be using up available CPU. Adding a call to Sleep() once in a while
freed up those cycles so the UI was again responsive.

Not sure what could be 'incorrect' about the structure of the code.
There's nothing that should have been able to lock out the UI
directly, so I believe it's just prioritization and CPU load. Perhaps
something to do with DB access.
 
J

J.B. Moreno

..NET/ C# MVP said:
Of what problem?

Of a textbox not appearing to be updated during a loop. The original
post was clear as to what he was getting and what he wanted, even if it
didn't include his actual usage.

And he received the correct answer: either somehow let the UI be
updated during the loop or simply let it happen after the loop
(depending on his needs).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top