3

In my application i have a WPF Window that has a DataGrid in it. In Window_Loaded event I get JSON data from server and after deserializing it and converting it into a List<T> i bind that to my data grid. Everything is working fine this way.

The problem:

But the request part of code takes time and the whole window seems white with no elements loaded including the data grid.I want to make the request part asynchronous just as in AJAX we show a loader image while something is loading and then using a callback function to show the content when it is loaded.

In step by step

  1. Load the window and elements data grid buttons etc..
  2. Make web request to server and show 'data is loading..' in a label possibly.
  3. Fire an event or something that notifies that data has loaded and then bind the data grid to list so that the application remains responsive and active during whole web request part.

Here is the current non-async code i am using in Window_Loaded event handler

 WebResponse response = req.GetResponse();
 Stream responseStream = response.GetResponseStream();
 StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
 string JSON = sr.ReadToEnd();
 List<MyObject> returnedData = JsonConvert.DeserializeObject<List<MyObject>>(JSON);

i found this link but i am not sure how it is applicable to my problem. Any help or idea for how to go about this is welcome.

0

3 Answers 3

4

Any reason you're not using the WebClient Class? Have a look at the WebClient.DownloadStringAsync Method.

Sign up to request clarification or add additional context in comments.

Comments

2

Use WebRequest.BeginGetResponse Instead of GetResponse, and assign the serialized result to List in callback method

Don't forgot that for assigning data from another thread to UI element created in another thread, you should send message to UI using dispatcher

Hope this helps

Comments

2

This code is a sample snippet from Silverlight, should be the applicable in WPF as well.

HttpWebRequest myRequest = WebRequest.CreateHttp("http://yoururl);
myRequest.Method = "GET";
myRequest.BeginGetResponse(GetResponseCallback, myRequest);

private void GetResponseCallback(IAsyncResult asynchronousResult)
{
    try
    {
        WebResponse resp = request.EndGetResponse(asynchronousResult);
        HttpWebResponse response = (HttpWebResponse)resp;
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        // Close the stream object
        streamResponse.Close();
        streamRead.Close();
        // Release the HttpWebResponse
        response.Close();

        //Do whatever you want with the returned "responseString"
        Console.WriteLine(responseString);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.