Search code examples
c#multithreadingasync-awaitwait

How can I delay running code on the UI thread in C#?


So I have a BackgroundWorker that does some stuff, then when it completes, RunWorkerCompleted has to update an ObjectListView.

Problem is, the user could be editing a cell in the ObjectListView. Trying to update it during cell editing results in absolute carnage, so i don't want to let RunWorkerCompleted do it's thing unless the user is not editing (i.e. ObjectListView.IsCellEditing is false).

What's the best way to do this?

I mean I could just set a timer to check IsCellEditing is false, but that seems a bit like a hammer to crack a nut and feels a bit dirty somehow.


Solution

  • You can use the OnCellEditFinishing event to know when editing has completed.

    So if IsCellEditing is true, then store the result of the calculation somewhere and in your OnCellEditFinishing handler, check for data that needs to be updated and make the update.