Dani
Dani

Reputation: 1047

Asynchronous UserControl

I’ve created my own charting library. So far everything works fine except the drawing part is not concurrent. How can I achieve that? Right now my screen is freezing while adding item to the chart control. I’ve tried to rewrite the adding function as Task.Run(()=> { adding to the ObservableCollection }) but without success.

Is it only making all method within the library async? If yes, what do I return since I need at least one Task?

Is there another way to create an asynchronous control, I mean in general? My wish is to have a control like the ListView where I can add items and the new items are added concurrent to the list.

Upvotes: 0

Views: 1965

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149558

UI controls are thread-affine, they can only be handled from the UI thread, that is why you're seeing Task.Run fail.

There is a general rule that you shouldn't occupy the UI with with CPU bound work for more than 50ms. If you have a large amount of data to process with UI controls, try batching the work into smaller work groups and freeing your UI thread between iterations.

In general, you shouldn't expose asynchronous wrappers over sync methods.

Let the user explicitly offload whichever work he has to a background thread.

Upvotes: 1

Related Questions