简体   繁体   English

等待正在停止程序执行

[英]await is stopping the execution of the program

I have a simple method which uses await .But the problem is that as soon as i reaches to the line the execution of the program is getting stopped , i dont know why.Here is my code.. 我有一个使用await的简单方法。但是问题是,一旦我到达该行,程序的执行就会停止,我不知道为什么。这是我的代码。

public async void GetUserdetails() {

var upn = "Custom Name";
var userLookupTask = activeDirectoryClient.Users.Where(
            user => user.UserPrincipalName.Equals(
                upn, StringComparison.CurrentCultureIgnoreCase)).ExecuteSingleAsync();

User userJohnDoe = (User)await userLookupTask;
Console.WriteLine(userJohnDoe.UserPrincipalName);
Console.WriteLine(userJohnDoe.DisplayName);
Console.ReadLine();
}

Any ways to get the execution running and see the values on Console.Please help.Thanks.. 任何使执行运行并在Console上查看值的方法。请帮助。谢谢。

I suspect your Console app is simply exiting. 我怀疑您的控制台应用程序正在退出。

To prevent your Console app from exiting, you should return a Task from GetUserdetails and call Wait on that task: 为了防止您的控制台应用程序退出,您应该从GetUserdetails返回一个Task并在该任务上调用Wait

static void Main()
{
  GetUserDetailsAsync().Wait();
}

static async Task GetUserDetailsAsync()
{
  ...
}

Note that Wait is not normally used in asynchronous programming; 注意,异步编程中通常不使用Wait it is usually only used once in the Main method of Console apps. 通常在控制台应用程序的“ Main方法中仅使用一次。

Alternatively, you can move the ReadLine into your Main method. 或者,您可以将ReadLine移到Main方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM