Xamarin 使用 async./wait VS await Task.Run(在 Xamarin 中) [英] Xamarin Using async./wait VS await Task.Run (in Xamarin)

查看:26
本文介绍了Xamarin 使用 async./wait VS await Task.Run(在 Xamarin 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 async/await 与 await Task.Run 但没有找到明确的结论.

I have been researching about async/await vs await Task.Run but have not found a definite conclusion.

  1. 是否异步/等待"?总是在与 UI 不同的线程上运行?如果是,为什么要使用await Task.Run"?

  1. Does "async/await" always run on a separate thread from the UI? And if yes why use "await Task.Run"?

根据我的研究,建议使用await Task.Run";用于 CPU 密集型任务和等待/异步"用于数据传输方案 (I/O),但其他人建议使用 await Task.Run(如果需要返回)或仅 Task.Run(触发和忘记)用于 Xamarin 中任何长时间运行的活动.

From my research, the recommendation is to use "await Task.Run" for CPU intensive tasks and "await/async" for data transfer scenarios (I/O) but others have recommended using await Task.Run (if needing the return) or just Task.Run (fire and forget) for ANY longer running activities in Xamarin.

那么await Task.Run"的最佳用途是什么?尤其是在 Xamarin 的上下文中,与 async/await 相比?

So what is the best use for "await Task.Run" especially in the context of Xamarin, in contrast with just async/await?

推荐答案

是否异步/等待"?总是在与 UI 不同的线程上运行?

Does "async/await" always run on a separate thread from the UI?

.

根据我的研究,建议使用await Task.Run";用于 CPU 密集型任务和等待/异步"用于数据传输场景 (I/O)

From my research, the recommendation is to use "await Task.Run" for CPU intensive tasks and "await/async" for data transfer scenarios (I/O)

这是 UI 应用程序的一个很好的一般准则.普通的 async/await 不使用额外的线程;它只是让您的 UI 保持响应.如果您有 CPU 绑定代码,那么您确实需要另一个线程,因此您使用 Task.Run,并且您可以使用 await 使用它当后台线程运行受 CPU 限制的代码时,它可以让您的 UI 保持响应.

That's a good general guideline for UI applications. Plain async/await doesn't use additional threads; it just keeps your UI responsive. If you have CPU-bound code, then you do need another thread, so you use Task.Run, and you can consume it using await which keeps your UI responsive while a background thread runs the CPU-bound code.

但其他人建议在 Xamarin 中使用 await Task.Run(如果需要返回)或仅使用 Task.Run(即发即弃)来处理任何长时间运行的活动.

but others have recommended using await Task.Run (if needing the return) or just Task.Run (fire and forget) for ANY longer running activities in Xamarin.

我不建议一劳永逸.除其他问题外,它还可以隐藏异常.我建议总是使用 await.

I don't recommend fire-and-forget. Among other issues, it can hide exceptions. I recommend always using await.

那么await Task.Run"的最佳用途是什么?尤其是在 Xamarin 的上下文中,与 async/await 相比?

So what is the best use for "await Task.Run" especially in the context of Xamarin, in contrast with just async/await?

上面的一般规则是有效的:对 I/O 操作使用 async/await,对 CPU 密集型操作使用 Task.Run.每隔一段时间,这条规则就会有一个例外.例如,有时 I/O 绑定操作是阻塞,并且它们不提供完全异步的 API;在这种情况下,即使操作在技术上受 I/O 限制而不是 CPU 限制,也应该使用 await Task.Run 来阻止后台线程而不是 UI 线程.

The general rule above is valid: use async/await for I/O operations, and Task.Run for CPU-bound operations. Every once in a while there's an exception to that rule. E.g., sometimes I/O-bound operations are blocking and they don't provide a fully asynchronous API; in that case, it would be proper use await Task.Run to block a background thread instead of the UI thread even though the operation is technically I/O-bound and not CPU-bound.

进一步阅读:

  • async best practices
  • Task.Run etiquette
  • Asynchronous MVVM data binding

这篇关于Xamarin 使用 async./wait VS await Task.Run(在 Xamarin 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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