UI响应 [英] UI Responsiveness

查看:53
本文介绍了UI响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF Async方法检索我的数据,获取数据的调用似乎很快。 但是我通过我的VM将数据作为ObservableCollection返回,然后绑定到我的网格。


但是在这段时间我的整个用户界面没有响应,我甚至无法在屏幕上移动它,直到所有网格都绑定到了ObservableCollection。


我的理解是对数据库调用使用await / async而对Cpu操作使用Task.Run? 但是如果我在Task.Run中包装我的数据库调用,基本上将它们放在他们自己的线程上,我的UI就会响应,虽然它们中没有数据,因为数据库调用仍然是
仍在执行,但至少UI可以是用户用于其他事情。


使用async / await方法时,加快UI响应速度的最佳方法是什么? 或者DbContext.Local(返回observablecollection)是什么减慢了它,而不是DBContext.LoadAsync?




nlm

解决方案

嗨nlm,


>> 速度的最佳方法究竟是什么?使用async / await方法时的UI响应能力? 或者是
DbContext.Local(它返回observablecollection)什么减慢了它,而不是DBContext.LoadAsync?


关于使用EF Async方法检索数据,是的,你可以使用await / async进行数据库调用和Task,Run进行CPU操作。


Task.Run对线程池中的方法(作为任务或任务< T>)进行排队(它使用/创建另一个线程来运行任务)



执行等待任务完成并返回其结果,而不会阻塞主线程,因为async关键字的神奇能力。


这是关于使用EF异步方法检索数据的相同线程,你可以看看:


< a href ="https://codereview.stackexchange.com/questions/80349/asynchronous-method-and-collection-binding"> https://codereview.stackexchange.com/questions/80349/asynchr onous-method-and-collection-binding



最好的问候,


Cherry





I am retrieving my data using EF Async methods, the calls getting the data seem quick.  However I return the data through my VM as ObservableCollection and then bind to my grids.

However during this time my entire UI is unresponsive, I can't even move it on the screen, until all the grids have been bound to their ObservableCollection.

My understanding is to use await/async for DB calls and Task.Run for Cpu operations?  However if I wrap my DB calls in Task.Run, essentially putting them on their own thread, my UI becomes responsive, though no data in them yet since the DB calls are still executing, but at least the UI can be used by the user for other things.

What exactly is the best way to speed up a UI responsiveness when using async/await methods?  Or is the DbContext.Local (which returns the observablecollection) what is slowing it down, not the the DBContext.LoadAsync?


nlm

解决方案

Hi nlm,

>>What exactly is the best way to speed up a UI responsiveness when using async/await methods?  Or is the DbContext.Local (which returns the observablecollection) what is slowing it down, not the the DBContext.LoadAsync?

About retrieving data using EF Async methods, yes, you can use await/async for DB calls and Task,Run for CPU operation.

Task.Run queues a method (as a Task or Task<T>) in the thread pool (it uses/creates another thread to run the task)

The execution waits at await for the task to finish and throws back its results, without blocking the main thread because of the async keyword's magic ability.

Here is the same thread about retrieving data using EF Async method , that you can take a look:

https://codereview.stackexchange.com/questions/80349/asynchronous-method-and-collection-binding

Best Regards,

Cherry


这篇关于UI响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆