views:

305

answers:

4

I have a scenario where I have a collection of objects bound to a datagrid in winforms. If a user drags and drops an item on to the grid, I need to add a placeholder row into the grid and kick off a lengthy async import process. I need to communicate the status of the async import process back to the UI, updating the row in the grid and have the UI remain responsive to allow the user to edit the other rows.

What's the best practice for doing this?

My current solution is: binding a thread safe implementation of BindingList to the grid, filled with the objects that are displayed as rows in the grid. When a user drags and drops an item on to the grid, I create a new object containing the sparse info obtained from the dropped item and add that to the BindingList, disabling the editing of that row. I then fire off a separate thread to do the import, passing it the newly bound object I have just created to fill with data. The import process, periodically sets the status of the object and fires an event which is subscribed to by the UI telling it to refresh the grid to see the new properties on the object.

Should I be passing the same object that is bound to the grid to the import process thread to operate on, or should I be creating a copy and merging back the changes to the object on the UI thread using BeginInvoke?

Any problems or advice with this implementation?

Thanks

A: 

it sounds like a reporting progress background worker will help you to do your import operation and ui reporting. you should read about this Background worker class on msdn

Chen Kinnrot
A: 

I think the BackgroundWorker will help you with this task (you can use also a separate thread with the right invoke() call on UI thread). The problem is the update of the UI done by all separate threads. You know, all update of UI need to be on the same thread the UI are born, so, creating a safing implementation of BindingList with a lock, and doing a lot of update, can cause the form to be unresponsive. If you use BackgroundWorker try to limit the use of ReportProgress, if the form it's lagging to much a safer way to improve the functionality is to use 2 datagrid. You can dispose them one next other like it's just one grid.

Bye

Guaido79
A: 

I would say it's better to create a copy and merging back the changes to the object on the UI thread since the other approach- using the original reference would require you to do more locking into the object and the bindinglist.

labadana
+1  A: 

ok ...

I see the flow of events being something like this:

  1. user drags and drops item to grid
  2. async process is kicked off
  3. user interface is updated to show "processing"
  4. callback handler gets response of async process
  5. async callback updates binding source
  6. async callback calls "databind" on the grid to refresh the view to contain the new data.

I would use a thread rather than a background worker but im pretty confident working with threads.

The background worker does simplify threading, that would be my recommended starting point if your are not confident.

That way you update the source and the ui together and the user can continue using the application during processing.

Wardy