2013-11-01 57 views
0

我正在使用Visual Studio 2010,因此安装了Visual Studio Async CTP (Version 3)以便使用异步并等待。我已经检查了包含的样本'AsnycSamplesVB'。这些样本一切正常。但是当我复制例如'AsyncResponsiveCPURun'到一个新的控制台应用程序中,它编译但只显示最初的行('Processing data ...'),然后停止。它调用ProcessDataAsync一次,但没有输出...?任何想法我做错了什么?这是我的控制台应用程序中的完整代码。这一切都来自微软的样品复制,除了在子主召唤:VS 2010中的异步不起作用

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 
+4

您不应该再使用CTP。 CTP需要更新/修补编译器,因此需要匹配正确的版本,VS的任何更新都可能会破坏ctp。更好地切换到支持异步/等待的VS2012,并通过FW4.0目标包支持FW4.0。 – igrimpe

+0

是的,这可能是最好的选择。我会很快做到的。谢谢! – K232

回答

2

找到在Async/Await FAQ on MSDN答案:

Sub Main() 
    AsyncResponsiveCPURun().Wait() 
End Sub 

我可以在控制台应用程序使用“等待”?

当然。但是,您不能在Main方法内使用“await”,因为 入口点不能标记为异步。相反,您可以在控制台应用程序中使用 其他方法中的“await”,然后如果您从Main调用 这些方法,则可以同步等待(而不是异步 等待)以完成它们。

public static void Main() 
{ 
    FooAsync().Wait(); 
} 

private static async Task FooAsync() 
{ 
    await Task.Delay(1000); 
    Console.WriteLine(“Done with first delay”); 
    await Task.Delay(1000); 
}