简体   繁体   中英

Web Scraping Event Handler Gets Stuck after pressing the “START” button - Windows Phone 7

I have written an Eventhandler (Asynchronous call) through which I download the source string (HTML) of any webpage, I enter the URL through a textbox and pass it to the method which calls the eventhandler, this works fine, except when:

I open the Application, do nothing and press the "START" button, open browser, copy any link to some page, press the "START" button again & run the application, and then when I paste that link into the text box and try scraping it, it gets stuck with my progress indicator in an infinite loop, I have tried refreshing page but it doesn't help, here's the code below:

 public void LoadSiteContent(string url)
 {
        //create a new WebClient object


        var indicator = new ProgressIndicator
        {
            IsVisible = true,
            IsIndeterminate = true,
            Text = "Downloading Source..."
        };

        try
        {
            SystemTray.SetProgressIndicator(this, indicator);

            WebClient client = new WebClient();

            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback2);

            client.DownloadStringAsync(new Uri(url));
        }
        catch (Exception ex)
        {
            if (ex is TimeoutException || ex is WebException)
            {
                MessageBox.Show("It seems there is no response from the remote server (are you connected to the internet?)");

            }
       }
 }

 private void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
 {

            if (!e.Cancelled && e.Error == null)
            {
              //Do whatever with the downloaded string (e.Result)
            }
 }
 private void button1_Click(object sender, RoutedEventArgs e)
 {


        string Entered_URL = "";

        Entered_URL = Enter_URL_Text.Text; //Stored from the textbox


        LoadSiteContent(Entered_URL);
 }

How do I get around this? Thanks!

I have tried your code and it works just fine. I think it "appears" to be stuck in infinite loop because of the ProgressIndicator . You need to change it's IsIndeterminate and Text property in your DownloadStringCallback2 method.

Try adding the following code to your DownloadStringCallback2 method:

private void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
    {
        var indicator = SystemTray.GetProgressIndicator(this);
        indicator.IsIndeterminate = false;
        indicator.Text = "";

        if (!e.Cancelled && e.Error == null)
        {
            MessageBox.Show("Page load complete.");

            //Do whatever with the downloaded string (e.Result)
        }
    }  

and see if it works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM