Winforms - 运行多个异步函数并等待最后一个完成 [英] Winforms - run several async functions and wait for the last one to complete

查看:70
本文介绍了Winforms - 运行多个异步函数并等待最后一个完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我们正在通过Async的大部分功能来改进我们的VB.NET解决方案。现在添加Async / Await非常简单,但我们对如何继续执行以下操作感到困惑。

在应用程序启动时,我们从Azure SQL DB预先填充了几(10)个数据表(我们有进展)在每个同步功能完成时增加其值的条形图)。我们可以在每个函数上添加Async / Await,但我们认为我们可以尝试同时运行它们,只需等待最后一个完成然后再继续(认为这会加快加载时间)。所有函数都是SQL SELECT命令。

我们已经阅读了相当多的内容,但没有找到我们需要的东西。我们以为我们只能使用Async / Await,没有后台工作者也没有任务。

将1个异步函数中的10个同步函数组合起来不是解决方案,因为我们以后仍然需要单独运行它们。

你会如何解决这个问题?非常感谢您的评论。

谢谢!

塞尔吉奥



我的尝试:



尝试使每个函数异步,但我们通过同时运行10个SELECT来获得初始性能提升。

解决方案

引用:

等待最后一个完成

这是一个令人困惑的声明。怎么说最后一个是哪一个?你有对Task变量中最后一个的引用吗?或者,你有一个列表,你只想看看它们何时完成?好吧,使用TPL(任务并行库)可以轻松解决这两个问题。

  var  lastTask = something; 

// 全部安排
lastTask.Wait( );

// 如果函数是异步的,则等待lastTask。

这将只等待最后一个任务,如果有任何其他任务挂起,它将跳过。但是,更好的选择是拥有 List< Task> 并为<$ c设置任务函数$ c>在其上调用WhenAll 。

  var  list = allTask​​s; 

Task.WhenAll(list).Wait(); // 或等待Task.WhenAll(list);

您已经在消费async / await模式,所以你知道async和await方法更好。但不知怎的,你似乎也有点困惑,

引用:

以为我们可以使用Async / Await C#中的异步/等待模式基于Task(System.Threading.Tasks命名空间)。因此,我认为您不能使用async / await应用任何内容,并跳过任务类型。 :-)



Task.WhenAll Method(System.Threading.Tasks)| Microsoft Docs [ ^ ]

引用:

10同时选择。

这就像对Azure SQL数据库的10个新请求,10个连接(即使后端可以利用连接池),然后是线程,线程池和上下文切换的开销。我不确定这是如何改善整体启动时间和性能的。是的,它可以提高响应能力和可伸缩性,并利用线程池线程,但仍然如此。 10 SELECT命令将花费相同的时间用于同步版本,但是,异步方法中的资源创建将会更便宜。


Hi!
We're improving our VB.NET solution by Async'ing most functions. Adding Async/Await is pretty easy now but we're confused on how to proceed with the following.
At application launch we pre-populate several (10) datatables from Azure SQL DB (we have a progress bar that increments its value as each sync function is done). We can add Async/Await on each function but we thought we could try and run them all at the same time and just wait on the last one to finish before we continue (thought this would speed up load time). All functions are SQL SELECT commands.
We've read quite a bit on this but didn't really find what we need. Thought we could use Async/Await only and no backgroundworkers nor tasks.
Combining the 10 Sync functions in 1 Async function is not a solution as we still need to run them individually later on.
How would you go about and nail this? Very much appreciate your comments.
Thanks!
Sergio

What I have tried:

Tried making each individual function Async but we're after initial performance gains by running the 10 SELECTs simultaneously.

解决方案

Quote:

just wait on the last one to finish

That is a bit of confusing statement. How do you say which is the last one? Do you have a reference to the last one in a Task variable? Or, you have a list of them, and you just want to see when all of them have completed? Well, both of these can be solved easily using the TPL (Task Parallel Library).

var lastTask = something;

// When all is scheduled
lastTask.Wait(); 

// Or await lastTask if the function is async.

This will wait only for the last task, if any other tasks are pending, it will skip. However, a better alternative is, to have a List<Task> and have the Task function for the WhenAll to be called on it.

var list = allTasks;

Task.WhenAll(list).Wait(); // or await Task.WhenAll(list);

You are already consuming async/await pattern, so you know that async and await approach is better. But somehow you also seem to be a bit confused in this,

Quote:

Thought we could use Async/Await only and no backgroundworkers nor tasks.

Async/await pattern in C# is based on Task (System.Threading.Tasks namespace). So, I don't think you can apply anything with async/await, and skip the Task type. :-)

Task.WhenAll Method (System.Threading.Tasks) | Microsoft Docs[^]

Quote:

10 SELECTs simultaneously.

That's like 10 new requests to the Azure SQL Database, 10 connections (even though backend can utilize connection pooling), and then an overhead of the threads, thread pools and context switching. I am unsure as to how this could have improved the overall startup time and performance. Yes, it can improve responsiveness and scalability, and utilize the thread pool threads, but, still. 10 SELECT commands are going to take the same amount of time they are going to take in sync version, but yes, resource creation is going to be a lot cheaper in asynchronous approach.


这篇关于Winforms - 运行多个异步函数并等待最后一个完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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