I’m stumped and I need your help.
I have a WPF application that calls a WEBAPI using ASYNC & AWAIT and my UI is experiencing delays.
Here is the code I’m using…
HttpResponseMessage response = await WEBAPI.GetHttpClient().GetAsync("api/COStateType/GetStateTypesByCountryID/" + Constants.COUNTRY_ID_USA);
response.EnsureSuccessStatusCode();
App.stateTypes = await response.Content.ReadAsAsync<List<coStateType>>();
The GetHttpClient code…
public static HttpClient GetHttpClient()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ABS2.WEBAPI.Uri"]);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
Here is the main issue…
During startup I display a splash screen with moving gears (animation). After implementing the call to the WEBAPI that animation is no longer smooth. I have read that the ASYNC calls using the HttpClient cause UI blocking and figure that was what is happening to my animation.
Here is the question…
Is there anyway around this issue? I’ve tried wrapping this call within a BackgroundWorker and/or Task but neither resolved the issue. Is there another way to communicate with the WEBAPI that will NOT cause this UI blocking? An I doing something wrong?
Many, many thanks for any and all help!!!
@Stef7 & @Panagiotis – You guys are right that the UI thread was blocked. I’m not sure I understand why an async call blocks the UI thread though; I’ll have to do more research.
I had already tried a Background thread but that didn’t resolve my issue until I implemented ContinueWith on the Task.
Thank you for your help.
Here is the final code.