I can't seem to get async to work the way I want. I'm pretty unfamiliar with it in general, but my research has led me to it. Here's my situation: I have a XAML form I'm loading that needs data from a web service. I can get the data, no problem. The issue is that I want the form to load with a "Please wait. Data loading..." screen that I have made, but nothing loads until the query finishes which can take up to 10 seconds, which is a poor user experience. Can anyone let me know what I'm doing wrong?
In my ViewModel main section, I call LoadData() to get the ball rolling.
I also had a thought...is async what I really want here?
public async void LoadData()
{
IsLoading = Visibility.Visible;
MonitorData downloadInfo = await GetWebApiInfo();
try
{
AssignDataToControls(downloadInfo);
IsLoading = Visibility.Collapsed;
Console.WriteLine("Loaded successfully.");
}
catch (Exception e)
{
IsLoading = Visibility.Collapsed;
Console.WriteLine("Error: " + e);
}
}
private void AssignDataToControls(MonitorData mon)
{
MainPanel.Clear();
mon.MainPanel.ToList().ForEach(x => MainPanel.Add(x));
Information = mon.Information;
MonitorText = mon.MonitorText;
ProgressData = mon.progList;
}
public async Task<MonitorData> GetWebApiInfo()
{
main = new MonitorData();
var url = "::::::::WEB API ADDRESS::::::::";
var request = (HttpWebRequest)WebRequest.Create(url);
//Task<HttpWebRequest> req = await (HttpWebRequest)WebRequest.Create(url);
//HttpWebRequest request = await GetWebRequest(url);
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream, Encoding.Unicode);
string responseFromServer = reader.ReadToEnd();
var deserializer = new JavaScriptSerializer();
main = deserializer.Deserialize<MonitorData>(responseFromServer);
reader.Dispose();
response.Dispose();
return main;
}
asyncmethods in your case is that you still need towaitbefore returning the control to the UI thus blocking the UI thread.