vb.net updating textbox 'live'

Soldato
Joined
27 Sep 2004
Posts
11,201
Location
The Ledge Beyond The Edge
I have a wpf application i am doing in vb 2008. When it runs it does stuff and is supposed to write to a text box as it happens.
I am using textbox1.text += "blahhhh"

But i am finding that it waits till it is completely finished everything before it updates and does them all at once.

Any help.
 
Will that interupt the thread?
I have got around it now by using application.doevents() after every time i write something to the text box.
 
Look into the BackgroundWorker class.
http://msdn.microsoft.com/en-us/library/8xs8549b.aspx

The behaviour you're experiencing is because you're carrying out the work on the UI thread, which means while your operation is running it can't update the TextBox.
The BackgroundWorker component runs the operation on a background thread and provides an easy way to update the UI as things progress.

Calling Application.DoEvents() will give you a similar behaviour, but what this does is interrupt the thread to process all Windows messages in the queue at that point. Using another thread will keep the UI thread free to process the messages as and when they arrive.

EDIT: I should also point out that, as you're using WPF, you want to look into using databinding to tie the value in the TextBox to a property on an object rather than simply appending to the Text property.
 
Last edited:
EDIT: I should also point out that, as you're using WPF, you want to look into using databinding to tie the value in the TextBox to a property on an object rather than simply appending to the Text property.

Yep, create a simple class with one property (your string value) and bind to it from your view. Add a method ReceiveUpdate(UpdateInfo update), which your background worker will invoke when it has something to say. Finally, implement INofityPropertyChanged on this class, and fire the PropertyChanged event whenever you receive an update from your backgroundworker.

In your background worker, invoke a delegate (Action<UpdateInfo>) passed in from your viewmodel. You need to do a cross-thread invoke dispatch here, so use App.Current.RootVisual.Dispatcher.

Although all that might seem like a faff, it's the correct way to do it IMO and your app will remain pretty well structured if you use this approach. You don't want to be calling Application.DoEvents... that's a throwback to the VB6 days.
 
Back
Top Bottom