且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

2异步任务并行和等待结果 - .NET

更新时间:2023-01-07 13:56:45

标记的方法为异步仅意味着该方法现在允许等待的方法中的一个或多个异步操作。但是关键字本身不会奇迹般地转法将异步之一。事实上,因为你不等待对任何在你的 tempWorker1 tempWorker2 方法,然后你的方法实际上是同步的

Marking a method as Async only means that the method is now allowed to Await on one or many asynchronous operations within the method. But the keyword itself doesn't magically turn the method into an asynchronous one. In fact, because you don't await on anything within your tempWorker1 and tempWorker2 methods then your methods are in effect synchronous.

支持文档:

这是异步方法通常含有AWAIT操作者的一次或多次出现,但由于没有等待前pressions不会导致编译错误。 如果异步方法不使用的await运算符标志着一个悬挂点,该方法执行作为同步方法确实,尽管异步修改。编译器发出对这些方法的警告。

An async method typically contains one or more occurrences of an await operator, but the absence of await expressions doesn’t cause a compiler error. If an async method doesn’t use an await operator to mark a suspension point, the method executes as a synchronous method does, despite the async modifier. The compiler issues a warning for such methods.

由于标记与异步关键字的方法没有方法中任何东西,等待是没有用的,编译器应该给你以下警告:

Because marking a method with the Async keyword without awaiting on anything within the method is not useful, the compiler should have given you the following warning:

警告的这种异步方法缺乏'等待'运营商等都将同步运行。考虑使用'等待'操作等待非阻塞API调用,或等待Task.Run(...)做在后台线程CPU绑定的工作。

Warning This async method lacks 'Await' operators and so will run synchronously. Consider using the 'Await' operator to await non-blocking API calls, or 'Await Task.Run(...)' to do CPU-bound work on a background thread.

在这种情况下,如果要并行的工作,你需要开始他们在单独的线程。而做到这一点的一种方式,就是通过调用 Task.Run 。公告称,通过使用 Task.Run ,真的不再需要你2种方法有异步关键字对它们设置,或者甚至仍然为此事功能。


In this case, if you want to parallelize the work, you need to start them in separate threads. And one way to do this, is by calling Task.Run. Notice that, by using Task.Run, there really is no longer a need for your 2 methods to have the Async keyword set on them, or to even remain a function for that matter.

所以,你可以实现你的目标是这样的:

So you can accomplish your goal like this:

   Private Sub tempWorker1()
      For i = 1 To 5000
         If i Mod 100 = 0 Then
            Console.WriteLine("From 1:{0}", i)
         End If
      Next
   End Sub

   Private Sub tempWorker2()
      For i = 1 To 5000
         If i Mod 100 = 0 Then
            Console.WriteLine("From 2:{0}", i)
         End If
      Next
   End Sub

然后你在并行执行的工作是这样的:

And then you execute the work in parallel like this:

  Dim task1 As Task = Task.Run(AddressOf tempWorker1)
  Dim task2 As Task = Task.Run(AddressOf tempWorker2)
  Await Task.WhenAll(task1, task2).ConfigureAwait(False)


注意:即使codeD正常,您可能没有注意到,并行执行你所期望的,如果每个执行的工作 tempWorkerX 方法并不太大。如果全方法的执行是能够在单一时间片或量子内执行,但仍可能出现像一种方法执行并完成其它甚至开始之前。请确保您的 tempWorkerX 方法并行执行足够的工作变得显着。


Note: Even if coded properly, you may not observe the parallel execution as you would expect if the work performed by each tempWorkerX method is not too great. If the execution of the full method is able to execute within a single time slice or quantum, it may still appear as though one method executes and completes before the other even starts. Make sure that your tempWorkerX methods perform enough work for the parallelization to become noticeable.