MSDN example on async/await - can't I reach the break point after the await call?

前端 未结 1 880
深忆病人
深忆病人 2021-01-22 03:25

In trying MSDN\'s example on async/await, why I can\'t reach a break point after the await operator ?

private static void Main(string[] args)
{
    AccessTheWebA         


        
相关标签:
1条回答
  • 2021-01-22 03:40

    You call your asynchronous method from a Console application's Main method without waiting for the async method to finish. As a result, your process terminates before your task has a chance to complete.

    Since you can't convert a Console application's Main to an asynchronous (async Task) method, you'll have to block on the asynchronous method, by calling Wait or .Result:

    private static void Main(string[] args)
    { 
        AccessTheWebAsync().Wait();
    }
    

    or

    private static void Main(string[] args)
    { 
        var webTask=AccessTheWebAsync();
        //... do other work until the resuls is actually needed
        var pageSize=webTask.Result;
        //... now use the returned page size
    }
    
    0 讨论(0)
提交回复
热议问题