- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经阅读了很多关于异步编程的文章,但我不确定一件事。我有第 3 方 winrt 库,用 C++ 编写,我想包装它。所以现在我有:
public Task LoginAsync(){
return Task.Run(winrtLibrary.Login();)
}
根据 Stephen Cleary 和 Stephen Toub 的博客,这不是好的解决方案。但是当我同步使用该方法时,我的 UI 将无响应并被阻塞。同步公开服务方法并在 UI 中使用 Task.Run 是否更好?
最佳答案
Stephen Toub 是什么意思
do not use Task.Run in the implementation of the method; instead, use Task.Run to call the method
你不应该使用 Task.Run
吗?隐藏异步方法背后的 CPU 绑定(bind)工作(Task
返回方法)。如果您需要包装外部代码,请将其隐藏在反射(reflect)此代码功能的界面后面。任何异步 I/O 都可以(并且应该)公开为 Task
返回方法,并且 CPU 绑定(bind)的工作必须使用适当的 API 公开。让代码的使用者自己决定如何使用该代码。当您碰巧也被消费者使用时,请使用Task.Run
运行您的同步代码(现在通过接口(interface)包装和公开)很明显您正在卸载 CPU 绑定(bind)工作。例如,在 UI 应用程序中,您应该调用 Task.Run
在您的 UI 层(而不是在您的 BL 甚至 DA 层的深处),很明显 UI 卸载了一些 CPU 绑定(bind)的工作。
Why do you think, that I shouldn't call Task.Run in BL? What if I have ViewModel, which references BL and BL references service layer (in my case is it wrapper).
我认为方法签名应该准确反射(reflect)方法的作用。我能做的就是将您重定向回 Cleary's article :
When a developer sees two methods in an API winrtLibrary.Login() and winrtLibrary.LoginAsync(), the convention is that they represent a naturally-asynchronous operation. In other words, the developer expects that winrtLibrary.LoginAsync() is the “natural” implementation and that winrtLibrary.Login() is essentially a synchronous (blocking) equivalent of that operation. That API implies that winrtLibrary.Login will at some point have the calling thread enter a wait state as it blocks for the naturally-asynchronous operation to complete.
如果您将方法签名为 public Task OffloadLoginToTheThreadPool()
,您仍然可以将同步代码隐藏在异步方法后面并遵循 Cleary 的经验法则.但我认为(显然 Cleary 也是)简单调用 Task.Run
的替代方案从 UI(或 Controller )中获取是一种更好的方法,它遵循清洁代码的原则。
关于c# - 在服务中使用 Task.Run 作为同步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116640/
这个问题在这里已经有了答案: Why use async and return await, when you can return Task directly? (8 个答案) 关闭 6 年前。
这个问题在这里已经有了答案: Are the days of passing const std::string & as a parameter over? (13 个答案) 关闭 8 年前。 我
我有一组标记为执行的通用任务。当任务完成时(使用 Task.WaitAny ),我将其添加到 ObservableCollection 中. 但是,问题出在 Task.WaitAny(...)行,上面
经过几个小时的努力,我在我的应用程序中发现了一个错误。我认为下面的 2 个函数具有相同的行为,但事实证明它们没有。 谁能告诉我引擎盖下到底发生了什么,以及为什么它们的行为方式不同? public as
这也与 Python 的导入机制有关,特别是与在函数内使用 import 有关。使用 Python 2.7.9 和 Fabric 1.10.0,创建以下三个文件: fabfile.py: from a
我有一个 Web API Controller (ASP.NET Core 5)。我的一些 API 是异步的,而其中一些不是。我接下来的问题是:使用 public **Task** WebApiMet
我们有类似下面的内容 List uncheckItems = new List(); for (int i = 0; i new Task(async () => await Process
我的代码没问题,但我想知道哪种风格更好,你会怎么看,我正在玩异步方法。 让我建立上下文: Parallel.ForEach(xmlAnimalList, async xml => {
这两种使用 await 的形式在功能上有什么区别吗? string x = await Task.Factory.StartNew(() => GetAnimal("feline")); Task m
我刚刚看到 3 个关于 TPL 使用的例程,它们做同样的工作;这是代码: public static void Main() { Thread.CurrentThread.Name = "Ma
考虑以下代码: public void CacheData() { Task.Run((Action)CacheExternalData); Task.Run(() => CacheE
Task> GetTaskDict() { return Task.FromResult(new Dictionary () ); } 此代码无法编译,因为我们无法在 Task> 到 Tas
我正在使用 ASP.NET 5 RC1 _MyPartial @model MyViewModel @using (Html.BeginForm())
当我尝试在 VS Code 中构建 C 任务时,它显示以下消息: 输出仅显示:The task provider for "C/C++" tasks unexpectedly provided a t
一些背景: 基本上归结为我希望能够在当前线程中“执行”任务。为什么? -我有一个任务创建程序例程,有一次我希望任务在后台任务中立即执行,而其他时候我希望使用 IOmniThreadPool 安排任务。
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我试图将run-sequence添加到我的gulp工作流程中,但是每次尝试执行使用run-sequence的任务时,都会出现此错误: 任务未配置为gulp上的任务。 根据运行序列的来源,这是由以下te
此代码在VS2015中给出了编译时错误 Error CS0266 Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'Sy
我正在尝试通过我的代码通过Google登出: suspend fun signOut(context: Context): Boolean = with(Dispatchers.IO) { t
谁能解释一下这两种说法的区别: Task bTask = backup.BackupCurrentDatabaseAsync() .ContinueWith(_ => CompressArch
我是一名优秀的程序员,十分优秀!