Jez Clark
Jez Clark

Reputation: 383

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.

Upvotes: 1

Views: 78

Answers (2)

CarCar
CarCar

Reputation: 680

Add this in your code where your background process CAN be paused safely. You can add as many points as you like.

bw.WaitOne(Timeout.Infinite);

And when you want it to trigger pauses at those points from your main thread... like when they begin editing a cell... use:

bw.Reset();

And to resume:

bw.Set();

Upvotes: 0

Gabriel Luci
Gabriel Luci

Reputation: 41008

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.

Upvotes: 2

Related Questions