Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to implement Async on my application through the await and async methods in NET 4.5 however, I need the web request to finish for the collections to filled but I do not want my UI to freeze up as I would like to transition to a loading page during the request.

HTML Request > Collections > UI.

private Task<string> WebRequestAsync(String urlforcollection)
        {
            return Task.Factory.StartNew(() =>
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlforcollection);
                request.Proxy = null;
                request.Method = "GET";
                WebResponse response = request.GetResponse();
                Stream mystream = response.GetResponseStream();
                htmlDocument.Load(mystream);
                response.Close();
                MessageBox.Show("Finished");
                return "";
            });
}


await WebRequestAsync("http://fartjuice.org.uk");

if (WebRequestAsync("http://fartjuice.org.uk").IsCompleted == true)
{
AddDataToCollections();
LoadToUI();
}

For some reason this does not work, I don't get the message show and my program loads to the page that displays the information without adding the binded information?

How can I do this? Thank you!.
Posted
Updated 18-Aug-13 12:27pm
v4
Comments
Sergey Alexandrovich Kryukov 18-Aug-13 18:12pm    
What is "async threading"? As if "sync threading" existed... :-)
—SA
SteveBaldwin21 18-Aug-13 18:27pm    
Haha. An Asynchronous operation then.:)
Sergey Alexandrovich Kryukov 18-Aug-13 18:35pm    
Great. I think you should better use thread, not task. And you should not wait the task for completion, or spin-wait by polling its completion status. This would be pull technology defeating purpose of threading. You should use push technology: let thread go and at the end perform Invoke to the UI thread (Dispatcher or System.Windows.Forms.Control, depending on what you are using)...

First of all, you need to tag the UI library or the application type you are using.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900