Updating listbox using a thread in
However I'm pretty sure Background Worker offers an event that automatically gets called on the Foreground thread and you should be able to update on this event without any problems.This is "Progress Changed" which can be fired by the background worker process by calling Report Progress.Anyone writing smart client applications will eventually run into a situation where some work must be performed asynchronously on a background thread.Long running computations, database access, web service calls and large file I/O are all good candidates.I realize what I'm doing is probably pretty silly, but I'm in the middle of learning WPF and would like to know how to do this. The listbox is being used to deliver status messages about the program while it's running.
In the method for that event, you can access the box and put the new value in. I add functions like the following so that I can add items to the list box from either the main thread or background threads.
I passed an anonymous delegate above: The bit between the is the method block.
This is called anonymous because only one is created and it doesnt have a name (usually you can do this using a lambda expression but in this case C# cannot resolve the Begin Invoke() method to call).
If the items come in bursts or require processing between adding each one, then you might want to think about running the processing behind the scenes (via a backgroundworker or a Task).
But, if you are just taking data and populating the list, then you are required to use the UI thread.