I need to do a RestRequest and get some JSON, I am not sure if my method is really async since there is still a little freeze in my UI when I use this method.
public async Task<List<MyObject>> Load()
{
var tcs = new TaskCompletionSource<List<Myobject>>();
var client = new RestSharp.RestClient("https://exampleapi.com");
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
[...]);
var request = new RestSharp.RestRequest("examp.json", Method.GET);
client.ExecuteAsync(request, response =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
List_ = new List<MyObject>();
List_ = JsonConvert.DeserializeObject<List<MyObject>>(response.Content);
tcs.SetResult(List_);
}
else
{
MessageBox.Show("Error");
}
});
return await tcs.Task;
}
Specially for this line of code :
List_ = JsonConvert.DeserializeObject<List<MyObject>>(response.Content);
is it really async ? because it seems to block the the UI . Can you tell me how can I make this function properly async ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…