![](/img/trans.png)
[英]Async/Await or Task.Run in Console Application/Windows Service
[英]Task Error when await in windows store application
嘿,我有一個方法:
public static async Task<myClassl> GetData()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http:sasa.com");
HttpResponseMessage response = await client.GetAsync("api/GetData");
myClassl data = await response.Content.ReadAsAsync<myClassl>();
return data ;
}
當我寫
myClassl t = await DataGetter.GetData();
我有:'await'運算符只能在異步方法中使用。 考慮使用“異步”修飾符標記該方法,並將其返回類型更改為“任務”。
您需要在編寫的位置標記方法async
:
// Add async to your calling method
private async Task SomeOtherMethod()
{
myClassl t = await DataGetter.GetData();
內部使用await
任何方法本身都必須是async
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.