views:

58

answers:

2

Hi everyone,

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?

+3  A: 

1 - No. Background tasks must synchronize to the UI thread before updating databound properties. This can be done by using a Task scheduled to TaskScheduler.FromCurrentSynchronizationContext.

If you're not on .NET 4.0 yet (e.g., Task isn't available), then it depends on how your "tasks" operate: a task with the concept of "completion" can use BackgroundWorker, and a task that runs indefinitely can use SynchronizationContext.Post.

2 - PropertyChanged is used by the binding to update its display. Not sure why it worked for you without calling it...

3 - BindingSource provides 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.

Stephen Cleary
Now I understand better the answer to question 2:I used to scroll to see the status of all the running tasksevery time I scrolled the view got invalidated and the statuses changed. when I didn't scroll, nothing changedThanks!
Paul
+1  A: 

If you are only running the one background task at a time, the BackgroundWorker could solve your problem, you can suscribe to the WorkCompleted event to update your UI. Otherwise if there are multiple threads going one using the Control.BeginInoke would solve your update UI issue , on WPF it will be Dispatcher.BeginInvoke.

The propertyChanged is called on the set of your property. But if you are using a collection as your BindingObject I would use the ObservableCollection.

jmayor