I’m trying to solve a classic problem –
I have a multi-threaded application which runs some
processor-intensive calculations, with a GUI interface.
Every time one of the threads has completed a task, I’d like to
update a status on a table
taskID | status
I use DataGridView and BindingList in the following way:
BindingList<Task> tasks;
dataGridView.DataSource = tasks
public class Task : INotifyPropertyChanged
{
ID{get;}
Status{get;set;}
}
Can a background thread safely update a task’s status?
and changes will be seen in the correct order in the GUI?
Second Question:
When do I need to call to PropertyChanged?
I tried running with and without the call, didn’t seem to bother..
Third Question:
I’ve seen on MSDN that dataGridView uses BindingSource
as a mediator between DataGridView.DataSource and BindingList
Is this really necessary?
1 – No. Background tasks must synchronize to the UI thread before updating databound properties. This can be done by using a
Taskscheduled toTaskScheduler.FromCurrentSynchronizationContext.If you’re not on .NET 4.0 yet (e.g.,
Taskisn’t available), then it depends on how your “tasks” operate: a task with the concept of “completion” can useBackgroundWorker, and a task that runs indefinitely can useSynchronizationContext.Post.2 –
PropertyChangedis used by the binding to update its display. Not sure why it worked for you without calling it…3 –
BindingSourceprovides various functionality; see What Is a BindingSource and Why Do I Need It?BTW: you’re welcome to ask more than one question on SO. This one question should really be three.