Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

freeze in UI during download content in WP7

I am new to WP7 coding. I am looking for sample code or guiding on the following task:

I had 3 html pages on remote server and I want to download the contents of each of the page and post it to 3 different panorama page (show as a textblock).

I had written 3 set of webclient to load the html page; it can be shown as where it suppose to be. My issue facing is, when/during the downloading, the UI thread is "freez" and unresponsive.

Can anyone guide me / show me the sample code that I can put the thread to background and once it finish, and shown in the UI?

This is the code that I use to download the HTML page.

private async void GetNewsIndex(string theN)
    {
        string newsURI = newsURL + theN;
        string fileName = theN + "-temp.html";
        string folderName = "news";

        prgBar01.Visibility = System.Windows.Visibility.Visible;

        try
        {
            Task<string> contentDataDownloaded = new WebClient().DownloadStringTaskAsync(new Uri(newsURI));

            string response = await contentDataDownloaded;

            WriteTempFile(theN, response.ToString());

            string contentData = ProcessDataToXMLNews(fileName, folderName);

            WritenewsIndexXMLFile(newsIndexURI, folderName, contentData);

            DisplayNewsIndex();

        }
        catch
        {
            //
        }
    }

I'd modified the above code as per suggestion by Sinh Pham, and it work perfectly as it expected. But, Since I need to run 3 instant of it to download the page from different souce at he same time; the code break. Any idea?

like image 805
Snake Chia Avatar asked Aug 13 '11 13:08

Snake Chia


2 Answers

Are you sure the UI freezes when downloading and not when processing the data? From your code it seems like you're only doing

WriteTempFile(theN, response.ToString());

string contentData = ProcessDataToXMLNews(fileName, folderName);

WritenewsIndexXMLFile(newsIndexURI, folderName, contentData);

DisplayNewsIndex();

on the UI thread. Try wrap them in a BackgroundWorker instead.

Edit: something like this:

Edit2: since your DisplayNewsIndex() function cause changes in the UI, it must be executed on the UI thread.

var bw = new BackgroundWorker();
bw.DoWork += delegate {
    WriteTempFile(theN, response.ToString());
    string contentData = ProcessDataToXMLNews(fileName, folderName);
    WritenewsIndexXMLFile(newsIndexURI, folderName, contentData);
    Deployment.Current.Dispatcher.BeginInvoke(() => {
        DisplayNewsIndex();
    });
};
bw.RunWorkerAsync();
like image 123
Sinh Pham Avatar answered Sep 30 '22 14:09

Sinh Pham


You can look into using HttpWebRequest. This works asynchronously on the background thread and is the same thing the WebClient uses internally. The WebClient simply abstracts the lower level work but, as you can see, it has the drawback of being returned on the UI thread. There's an example of how to use HttpWebRequest here.

like image 43
keyboardP Avatar answered Sep 30 '22 13:09

keyboardP