Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用一系列异步方法后Winforms应用程序冻结_C#_Winforms_Asynchronous_Async Await - Fatal编程技术网

C# 调用一系列异步方法后Winforms应用程序冻结

C# 调用一系列异步方法后Winforms应用程序冻结,c#,winforms,asynchronous,async-await,C#,Winforms,Asynchronous,Async Await,我正在使用异步方法开发winforms应用程序。在一个事件上,我调用多个异步方法系列。 不幸的是,应用程序冻结了。我的异步调用都在等待,但我无法找出导致死锁的原因 Winforms块 private async void sendMessages_Click(object sender, EventArgs e) { await ServiceFacade.SendMessage("Hi to CEO"); await ServiceFacad

我正在使用异步方法开发winforms应用程序。在一个事件上,我调用多个异步方法系列。 不幸的是,应用程序冻结了。我的异步调用都在等待,但我无法找出导致死锁的原因

Winforms块

    private async void sendMessages_Click(object sender, EventArgs e)
    {
         await ServiceFacade.SendMessage("Hi to CEO");
         await ServiceFacade.SendMessage("Hi to Manager");
         await ServiceFacade.SendMessage("Hi to Team Leader");
    }
我的库类,它将数据发送到api

   public class ServiceFacade
   {

   public  async Task SendMessage(string message)
   {

       //validates and formats message then calls PostAsync to send it on api service

       await PostAsync(string message);
   }

   private static async Task PostAsync(string message)
   {

       HttpWebRequest request = (HttpWebRequest)WebRequest.Create("hostname.com")
       //set up REQUEST ...
       var response = (HttpWebResponse)await request.GetResponseAsync();
   }

   }

@我等了大约30分钟,我的应用程序仍然冻结。我还将请求设置为60秒。如果实际代码可能更复杂,您是否在异步流中的任何位置使用Task.Wait()?听起来你已经意识到了这一点,所以我想没有。@john我没有,因为它会阻塞上下文。我所有的任务都在等待着。我很困惑这是如何导致僵局的。我只是想确认一下,我在我的末端复制了它,但我不确定它为什么会发生。我会看看我能找到什么:)这似乎与这里接受的答案有关(增加允许我所有请求运行的最大连接数):-我怀疑异步使用它时存在错误,这会阻止它正确返回错误/等待。我建议切换到HttpClient(我没有遇到过这些问题。)