Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding in WinForms performing async data import

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

like image 788
burnside Avatar asked Nov 15 '22 12:11

burnside


1 Answers

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.

like image 77
War Avatar answered Dec 20 '22 22:12

War