c# - 未响应显示在标题栏中

标签 c#

我正在调用服务器 API 并等待响应。我只能在收到服务器响应后才能继续。但有时会收到服务器响应,只是在一段时间延迟后,我的标题栏显示 Not responding 消息。我该如何解决这个问题。

我的代码块是

    private string SubmitDocument(XmlDocument xmlinput, string URL)
    {
        try
        {
            // get the data from the xml document into a byte stream
            Byte[] byteinput = System.Text.Encoding.UTF8.GetBytes(xmlinput.OuterXml);

            // instantiate a web client
            System.Net.WebClient myWebClient = new System.Net.WebClient();
            myWebClient.Headers.Add("Content-Type", "text/xml");

            byte[] responseArray = myWebClient.UploadData(URL, byteinput);


            string responseString = Encoding.ASCII.GetString(responseArray);


            return responseString;
        }
        catch(Exception ex)
        {                
            return null;
        }
    }

谢谢

最佳答案

与其阻塞 UI,不如在后台线程(可能是 BackgroundWorker,也许是 ThreadPool)上完成这项工作。由于听起来您想模拟同步,只需禁用 UI 控件即可 - 不要挂起表单。然后在您确实从服务器获得响应时更新 UI。

关于c# - 未响应显示在标题栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1221632/

相关文章:

c# - Visual Studio - ExcludeFromCodeCoverage

c# - 装箱后更改结构

c# - 解决编译器错误 CS1519 : Invalid token ',' in class, 结构或接口(interface)成员声明

c# - 是否可以访问接口(interface)类型的其他成员?

c# - 无法从根提供程序解析范围服务 'Microsoft.AspNetCore.Identity.UserManager` 1[IdentityServerSample.Models.ApplicationUser]'

c# - TextBox 不支持系统十进制(点或逗号)

c# - RabbitMQ服务器认为它已经发送了消息,RabbitMQ客户端不同意

c# - 如何在 asp.net 中获取没有查询字符串值的 url?

c# - C#/Java 中动态对象的限制

regex - 用于验证名称和姓氏的正则表达式?