我正在使用 Visual Studio 2010,因此安装了Visual Studio Async CTP(版本 3)以便使用 async 和 await。我检查了包含的样本“AsnycSamplesVB”。这些样本都工作正常。但是当我将例如'AsyncResponsiveCPURun'复制到一个新的控制台应用程序中时,它会编译但只显示初始行('处理数据......')然后停止。它调用 ProcessDataAsync 一次但没有输出......?任何想法我做错了什么?这是我的控制台应用程序中的完整代码。除了 Sub Main 中的调用外,它都是从 Microsoft 示例中复制的:
Module Module1
Sub Main()
AsyncResponsiveCPURun()
End Sub
Public Async Function AsyncResponsiveCPURun() As Threading.Tasks.Task
Console.WriteLine("Processing data... Drag the window around or scroll the tree!")
Console.WriteLine()
Dim data As Integer() = Await ProcessDataAsync(GetData(), 16, 16)
Console.WriteLine()
Console.WriteLine("Processing complete.")
End Function
Public Function ProcessDataAsync(ByVal data As Byte(), ByVal width As Integer, ByVal height As Integer) As Threading.Tasks.Task(Of Integer())
Return Threading.Tasks.TaskEx.Run(
Function()
Dim result(width * height) As Integer
For y As Integer = 0 To height - 1
For x As Integer = 0 To width - 1
Threading.Thread.Sleep(10) ' simulate processing cell [x,y]
Next
Console.WriteLine("Processed row {0}", y)
Next
Return result
End Function)
End Function
Public Function GetData() As Byte()
Dim bytes(0 To 255) As Byte
Return bytes
End Function
End Module