gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:13:53 24 4
gpt4 key购买 nike

我正在调用服务器 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com