c# - 在任务完成时使用不带异步的等待来处理任务

标签 c# vb.net multithreading asynchronous async-await

刚刚开始异步编程,我的最终目标是异步获取给定文件集的 sha1 哈希值,然后使用计算出的哈希值继续处理所述文件。我发现以下 MSDN 博客文章讲述了如何在任务完成时处理任务结果:Link

整篇文章是用C#写的,我的项目是用VB.NET写的。我试图自己将 C# 代码重写为 VB,但是我一定是错过了一个关键步骤,或者我没有完全理解 Async/Await 编程的过程/语法。

我在以下几行收到以下错误:

Error 1 'Await' can only be used when contained within a method or lambda expression marked with the 'Async' modifier.

Dim t As Task(Of Integer) = Await bucket
Dim result As Integer= Await t

Dim t As Task(Of String) = Await bucket
Dim result As String = Await t

我可以通过将 Async 添加到包含的 Sub 声明来消除错误。但是,如果我这样做,我会收到另一个错误,因为包含方法是 main() 并且它是一个控制台应用程序。

Error 1 The 'Main' method cannot be marked 'Async'.

所以我想我的问题是,如何在不使包含方法异步的情况下使用 Await 异步任务?我下面的代码只是在 WinForms 项目中实现的测试器,我宁愿远离非 native .NET 片段。

下面是我从 C# 转换而来的完整代码,以及我为文件计算 sha1 散列的一小段代码:

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks


Module Module1

    Async Sub main()
        ' From the MSDN article
        Dim taskArr As Task(Of Integer)() = {Task(Of Integer).Delay(3000).ContinueWith(Function(x) 3I), _
                                           Task(Of Integer).Delay(1000).ContinueWith(Function(x) 1I), _
                                           Task(Of Integer).Delay(2000).ContinueWith(Function(x) 2I), _
                                           Task(Of Integer).Delay(5000).ContinueWith(Function(x) 5I), _
                                           Task(Of Integer).Delay(4000).ContinueWith(Function(x) 4I)}

        For Each bucket As Task(Of Task(Of Integer)) In Interleaved(taskArr)
            Dim t As Task(Of Integer) = Await bucket  ' Error Here
            Dim result As Integer = Await t  ' Error Here
            Console.WriteLine("{0}: {1}", DateTime.Now, result)
        Next

        'My bit of code for computing the file hashes
        Dim tasks As New List(Of Task(Of String))
        Array.ForEach(New DirectoryInfo("C:\StackOverflow").GetFiles("*", SearchOption.AllDirectories), Sub(x) tasks.Add(getHashAsync(x)))

        For Each bucket As Task(Of Task(Of String)) In Interleaved(tasks)
            Dim t As Task(Of String) = Await bucket  ' Error Here
            Dim result As String = Await t  ' Error Here
            Console.WriteLine(result)
        Next
    End Sub

    ' Original C# code that I converted to VB myself
    Public Function Interleaved(Of T)(tasks As IEnumerable(Of Task(Of T))) As Task(Of Task(Of T))()
        Dim inputTasks As List(Of Task(Of T)) = tasks.ToList()
        Dim buckets() As TaskCompletionSource(Of Task(Of T)) = New TaskCompletionSource(Of Task(Of T))(inputTasks.Count - 1I) {}
        Dim results() As Task(Of Task(Of T)) = New Task(Of Task(Of T))(buckets.Length - 1I) {}

        For i As Integer = 0I To buckets.Count - 1I Step 1I
            buckets(i) = New TaskCompletionSource(Of Task(Of T))()
            results(i) = buckets(i).Task
        Next

        Dim continuation As New Action(Of Task(Of T))(Function(completed As Task(Of T))
                                                          Dim bucket As TaskCompletionSource(Of Task(Of T)) = buckets(Interlocked.Increment(-1I))
                                                          Return bucket.TrySetResult(completed)
                                                      End Function)

        For Each inputTask As Task(Of T) In inputTasks
            inputTask.ContinueWith(continuation, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)
        Next

        Return results
    End Function

    ' Get the sha1 hash of the file
    Private Async Function getHashAsync(fle As FileInfo) As Task(Of String)
        Using strm As New IO.FileStream(fle.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
            Return Await New Task(Of String)(Function() As String
                                                 Dim sb As New Text.StringBuilder()
                                                 Using sha1 As New System.Security.Cryptography.SHA1CryptoServiceProvider()
                                                     Array.ForEach(sha1.ComputeHash(strm), Sub(x As Byte) sb.Append(x.ToString("x2")))
                                                 End Using
                                                 Return sb.Append(" | ").Append(fle.FullName).ToString
                                             End Function)
        End Using

    End Function 

End Module

最佳答案

await 只是等待 Task 结果的一个选项,第二个是 Task 静态方法用法,用于同步等待结果所有的任务。
据我所知,您正在创建 TTasks 列表,如下所示:

var list = new List<Task<Task<string>>>();

如果是这样,您可以做的最简单的事情就是展平任务列表,然后用 Task.WaitAll() 等待其中的所有任务。方法调用两次,如下所示:

var list = new List<Task<Task<string>>>();

Task.WaitAll(list.ToArray());
// now we aggregate the results
var gatheredTasks = list.Select(t => t.Result);
Task.WaitAll(gatheredTasks.ToArray());
foreach (var task in gatheredTasks)
{
    Console.WriteLine(task.Result);
}

在这种情况下,您将获得 TPL 的所有奖励,因为有很多技术用于平衡工作等,您将获得所需的所有结果。

如果您想按出现的顺序获得结果,您可以使用 Task.WaitAny() 编写一个循环方法,但在我看来,这不是一个好的选择。

关于c# - 在任务完成时使用不带异步的等待来处理任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085745/

相关文章:

C# List<string[]> 到 List<object[]> 的转换

c# - 如何在 ASP.net Core 中创建 "decorator abbreviation"

c# - 当我拥有基类型中另一个属性的 BsonId 属性时,有没有办法在派生类中使用 "Id"属性?

java - 借助枚举 VB.NET 获取设置

vb.net - 方法的类型签名与 PInvoke 不兼容

multithreading - 在阻塞主线程中使用 TEvent 和 MsgWaitForMultipleObjects

c# - 是否可以将变量传递到 for 循环中?

c# - 如何绑定(bind)到 CheckedListBox.SelectedItems.Count

iphone - iOs后台线程中的函数调用

Android Wear MessageAPI - 无法在 UI 线程上发送消息