我正在调用服务器api并等待响应.我只有在收到服务器的回复后才能继续.但有时服务器响应只是在一段时间延迟后收到,而我的标题栏显示无响应消息.我怎么能解决这个问题.
我的代码块是
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;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
而不是阻止UI,在后台线程(也许BackgroundWorker可能ThreadPool)上执行此操作.由于听起来您想要模拟同步,因此只需禁用 UI控件 - 不要挂起表单.然后,当你更新UI 做得到服务器的响应.
| 归档时间: |
|
| 查看次数: |
915 次 |
| 最近记录: |