等待服务器响应PostAsJsonAsync

时间:2019-03-07 10:06:21

标签: c# winforms

所以,我有这段代码,我想在服务器没有响应时冻结表单。

var response = await client.PostAsJsonAsync(url, _savedClient);

我该怎么办?

        try
        {
            frmMainForm._idErro = 1;
            using (var client = ReturnHTTP.HTTP())
            {
                frmMainForm._idErro = 11;
                var response = await client.PostAsJsonAsync(url, _savedClient);
                //MessageBox.Show();
                if (response.IsSuccessStatusCode)
                {
                    notifyIcon.ShowBalloonTip(1000, "Informação", "Cliente atualizado com sucesso", ToolTipIcon.Info);
                    notifyIcon.Visible = true;
                    frmMainForm._idErro = 3;
                    frmMainForm._tableContract.Clear();
                    List<Client> client_status = await response.Content.ReadAsAsync<List<Client>>();
                    frmMainForm._tableContract = ConvertDataTable.ToDataTable(client_status);
                    DialogResult = DialogResult.OK;

                    DadosParaEmail();

                    _gravou = true;
                    Close();
                }
                else
                {
                    var result = await response.Content.ReadAsStringAsync();
                    string a = System.Reflection.MethodBase.GetCurrentMethod().Name;
                    Error.saveErro(a, result);
                }
            }
        }
        catch (Exception ex)
        {
            string a = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string exc = ex.Message;
            Error.saveErro(a, exc);
        }

1 个答案:

答案 0 :(得分:1)

如果要在等待响应时阻止当前线程,可以尝试以下操作:

var response = client.PostAsJsonAsync(url, _savedClient).Result;