按下“开始"按钮后,网页抓取事件处理程序卡住了按钮 - Windows Phone 7 [英] Web Scraping Event Handler Gets Stuck after pressing the "START" button - Windows Phone 7
问题描述
我编写了一个 Eventhandler(异步调用),通过它我下载任何网页的源字符串 (HTML),我通过文本框输入 URL 并将其传递给调用 eventhandler 的方法,这工作正常,除非:
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!
推荐答案
我已经试过你的代码,它工作得很好.我认为它似乎"因为 ProgressIndicator
而陷入无限循环.您需要在 DownloadStringCallback2
方法中更改它的 IsIndeterminate
和 Text
属性.
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.
尝试将以下代码添加到您的 DownloadStringCallback2
方法中:
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.
这篇关于按下“开始"按钮后,网页抓取事件处理程序卡住了按钮 - Windows Phone 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!