Thursday, 22 August 2013

c# WebBrowser DocumentText works once but not in a loop?

c# WebBrowser DocumentText works once but not in a loop?

I am interested in checking the content of a website, the content changes
frequently and when I view the website on any browser, it refreshes itself
every 30 seconds. I want to know when the content has changed.
I am using winforms and I want to just click a button to start a loop,
every 30 seconds. I don't want to hit the website too frequently, in fact
the web pages own refresh is more than enough for my needs.
My code works when I click a button (btnCheckWebsite), if I wait a minute
and then click btnCheckWebsite again, my message box pops up because the
web page has changed. This is great however I want to do this in a while
loop. When I un-comment my while loop, the DocumentText never changes. I
have debugged it and for some reason it's the same text every time, even
when the web page has changed in the real world, it stays the same in my
code.
So my question is why can't I use a loop and what can I do instead to run
this repeatedly with any input from me?
As a bonus, I would like to remove the .Refresh() I added this because it
won't work without it however as I understand it, this refreshes the whole
page. When I use a browser I see the page updating even when I don't
refresh the whole page.
Just for background info, I did start by having a WebBrowser control on my
form, the page refreshes automatically. I used the same code and have the
same problem, interestingly, the WebBrowser control on my windows form
refreshes by itself no problem, until I click btnCheckWebsite and then it
stops refreshing! Also I know about webrequest but I don't know how to use
it for my purposes.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Check_Website
{
public partial class Form1 : Form
{
public WebBrowser _memoryWebBrowser = new WebBrowser();
String _previousSource = "emptySource";
public Form1()
{
InitializeComponent();
_memoryWebBrowser.Navigate(new Uri("http://www.randomurl.com/"));
}
private void btnCheckWebsite_Click(object sender, EventArgs e)
{
//I want to un-comment this while loop and let my code run
itself but it stops working
//when I introduce my while loop.
//while (1 < 2 )
//{
//Thread.Sleep(30000);
checkWebsite();
//}
}
private void checkWebsite()
{
//Why do I need this refresh? I would rather not have to hit
the web page with a refresh.
//When I view the webpage it refreshed with new data however
when I use a WebBrowser
//the refresh just doesn't happen unless I call Refresh.
_memoryWebBrowser.Refresh();
Thread.Sleep(500);
while (((_memoryWebBrowser.ReadyState !=
WebBrowserReadyState.Complete) ||
(_memoryWebBrowser.DocumentText.Length < 3000)))
{
Thread.Sleep(1000);
}
String source = _memoryWebBrowser.DocumentText;
if ((source != _previousSource) && (_previousSource !=
"emptySource"))
{
//Hey take a look at the interesting new stuff on this web
page!!
MessageBox.Show("Great news, there's new stuff on this web
page www.randomurl.co.uk!!" );
}
_previousSource = source;
}
}
}

No comments:

Post a Comment