Reputation: 391
Okay, the goal is to attempt to detect when a web page has been updated, and display the updated page in a WebBrowser control. For instance, say that I know Amazon will make the Playstation 4 available today, but I don't know when. I put the address of the Amazon PS4 page in my form, and the program periodically launches a WebBrowser to that page and saves the HTML to a list of string. Next time around when it launches the browser, it will check the results against my list. If the list already contains this particular string, the WebBrowser is closed and we wait again using Thread.Sleep.
private void Launch()
{
while (blnWorking)
{
if (intWindowCount < intMaxCount)
{
ShowBrowserUri(GetBrowserUri());
}
if (intWait > 0)
Thread.Sleep(intWait);
}
}
delegate void ShowBrowserUriDelegate(Uri uri);
private void ShowBrowserUri(Uri uri)
{
if (InvokeRequired)
{
ShowBrowserUriDelegate del = new ShowBrowserUriDelegate(ShowBrowserUri);
Invoke(del, uri);
}
else
{
CookieContainer cc = GetUriCookieContainer(uri);
CookieCollection cc1 = new CookieCollection();
if (cc != null)
{
cc1 = cc.GetCookies(uri);
}
THBrowser thb = new THBrowser(uri, cc1, this); //THBrowser is a form with a WebBrowser control
//THBrowser calls the Navigate method of its WebBrowser control
intWindowCount++;
thb.Show();
thb.SendToBack();
}
}
The problem is, my program seems to be randomly opening the website in Firefox (my default browser) instead of using the THBrowser/WebBrowser. I've stepped through, and it seems to occur every time I step out of the Thread.Sleep.
It also may be relevant that OnDocumentComplete for my WebBrowser compares the current HTML to my List of HTML strings and closes the control if the page contents are already contained in my list.
I don't have the most recent version in front of me, but that part goes something like...
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
return;
if (ResultsList.Contains(this.webBrowser1.Document.Body.InnerHtml)
this.Close();
else
ResultsList.Add(this.webBrowser1.Document.Body.InnerHtml);
}
I think this may be relevant because the THBrowser opens briefly, disappears, then a new tab is immediately opened in Firefox.
I'm pretty sure the Thread.Sleep has something to do with it. Although, I had seen this problem occasionally before introducing the Thread.Sleep, it has multiplied 100 fold since.
Maybe Thread.Sleep isn't the best way to space out my intervals. Maybe I'm just going about this completely wrong. Any suggestions are appreciated.
Upvotes: 0
Views: 876
Reputation: 134015
First, it's highly unlikely that Thread.Sleep
has anything to do with the page sometimes loading in Firefox rather than in the WebBrowser
control.
Thread.Sleep
is not typically the best way to do things periodically. You'd probably be better off with a timer. If you're using Windows Forms, that's System.Windows.Forms.Timer. Set the Interval
to whatever delay you want, and have the Tick
event update the browser.
I'm not sure why you're creating a new WebBrowser
control every time. Seems you'd be better off creating it just once and then calling Refresh
method.
Finally, do you really need a WebBrowser
? You might be better off just using WebClient.DownloadString to download the HTML.
Upvotes: 2