使用await而不使用异步来处理任务完成

时间:2015-08-19 02:47:49

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

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

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

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

  

错误1'Await'只能在包含在标有“Async”修饰符的方法或lambda表达式中时使用。   

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

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

  

错误1'Main'方法不能标记为'Async'。

所以我想我的问题是,如何在不使用包含方法Async的情况下使用Await异步任务?我下面的代码只是一个在WinForms项目中实现的测试程序,我宁愿远离非本机.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

2 个答案:

答案 0 :(得分:2)

我不熟悉VB.NET和C#,但我认为这可以通过async console programs来回答。实质上,您希望将异步任务包装在AsyncContext中,并使用任务运行器运行它。有关另一个好例子,请参阅here

编辑: 如果您希望保留原生.NET,则可以创建自己的任务运行程序。基于一个基本的例子,我想它看起来像这样:

static void Main(string[] args)
{
    Task t = AsyncMain(args);
    t.Wait();
}

async static Task AsyncMain(string[] args)
{
    await Task.Delay(1000); // Real async code here.
    Console.WriteLine("Awaited a delay of 1s");
    return;
}

答案 1 :(得分:2)

await只是等待Task结果的一个选项,第二个是Task静态方法用法,用于同步等待所有任务的结果。<登记/> 据我所知,您正在创建Tasks Tasks T var list = new List<Task<Task<string>>>(); 的列表,如下所示:

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);
}

如果是这样,你可以做的最简单的事情是压平任务列表,然后用Task.WaitAll()方法调用其中的所有任务,如下所示:

TPL

在这种情况下,您将获得$name = mysqli_escape_string("$_POST['name']"); 的所有奖励,因为有很多技术用于平衡工作等等,您将获得所需的所有结果

如果你想按照它们的外观顺序得到结果,你可以用Task.WaitAny()方法写一个循环,但在我看来这不是一个好的选择。