
在另一個方法里這樣呼叫可以嗎 主要實作呼叫的時候加載等待視窗
this.splashScreenManager1.ShowWaitForm();
Task<string> xmlstring = GetPatientList(pid);
this.splashScreenManager1.CloseWaitForm();
uj5u.com熱心網友回復:
報錯怎么處理?uj5u.com熱心網友回復:
你這個DCLInterface不是異步方法,如果方法是異步方法,你只需要在await之前彈出加載框,await后關閉加載框就好了。uj5u.com熱心網友回復:
給你一個異步方法public static class CallWebAPI
{
public static async Task<string> APIPost(string url, string data)
{
string result = string.Empty;
//設定HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//創建HttpClient(注意傳入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
//傳遞單個值
{"", data}//鍵名必須為空
//傳遞物件
//{"name","hello"},
//{"age","16"}
});
//await異步等待回應
var response = await http.PostAsync(url, content);
//確保HTTP成功狀態值
response.EnsureSuccessStatusCode();
//await異步讀取最后的JSON(注意此時gzip已經被自動解壓縮了,因為上面的AutomaticDecompression = DecompressionMethods.GZip)
result = await response.Content.ReadAsStringAsync();
}
return result;
}
static async void APIGet(string url)
{
//創建HttpClient(注意傳入HttpClientHandler)
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
using (var http = new HttpClient(handler))
{
//await異步等待回應
var response = await http.GetAsync(url);
//確保HTTP成功狀態值
response.EnsureSuccessStatusCode();
//await異步讀取最后的JSON(注意此時gzip已經被自動解壓縮了,因為上面的AutomaticDecompression = DecompressionMethods.GZip)
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
uj5u.com熱心網友回復:
DCLInterface是對方web服務方法uj5u.com熱心網友回復:
DCLInterface 不是異步方法,不能使用 await。你可以使用 async 對 DCLInterface 做一下封裝,再對封裝的函式使用 await。ps:加載窗與異步有關系嗎?不是所有場景都應使用異步,否則容易將簡單問題復雜化。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/188483.html
標籤:C#
上一篇:swagger
