I am working on a small app in C# that refreshes a web page until some conditions are met. I have a “Fire” = start refreshing button and a “Stop!” button that is supposed to stop the operation. My problem is that it takes 2 tries to click the stop button instead of 1. Below is my code:
Updated code with timer. Still I think there can be some better use of the timer, I think that it doesn’t update every second after the first 2-3 refreshes or doesn’t refresh at all. Is there any flaw in my code that I can’t detect?
private void FireButtonClick(object sender, EventArgs e)
{
try
{
if (webBrowser1.Url.ToString().StartsWith("some url"))
{
_stopped = false;
_timer.Tick += new EventHandler(RefreshBrowser);
_timer.Interval = (1000) * (1);
_timer.Enabled = true;
_timer.Start();
}
else
{
MessageBox.Show("You must logon first.");
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void RefreshBrowser(object sender, EventArgs e)
{
string content = "disabled";
string baseUrl = @"http://some url";
string newUrl = string.Empty;
string buttonXpath = @"/html/body/div/div[6]/table/tr/td[2]/table/tr/td/table/tr/td/table/tr[3]/td[2]/div[4]/a";
webBrowser1.Refresh();
_proceed = false;
if (!content.ToLower().Equals("disabled") && !_stopped)
{
if (!_stopped)
{
HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml(webBrowser1.DocumentText);
HtmlNode node = htmlDocument.DocumentNode.SelectSingleNode(buttonXpath);
content = node.GetAttributeValue("disabled", string.Empty);
newUrl = node.GetAttributeValue("href", string.Empty);
}
}
else
{
webBrowser1.Navigate(baseUrl + newUrl);
}
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
_proceed = true;
urlTextBox.Text = webBrowser1.Url.ToString();
}
private void MainPageButtonClick(object sender, EventArgs e)
{
try
{
webBrowser1.Navigate(_mainPage);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GoButtonClick(object sender, EventArgs e)
{
try
{
webBrowser1.Navigate(urlTextBox.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void StopButtonClick(object sender, EventArgs e)
{
_timer.Stop();
_proceed = true;
_stopped = true;
}
}
You should really modify your code to use a timer. When you misuse the GUI thread and make periodic calls to
Application.DoEvents();the click can only handled at that place meaning you have to enter the loop to process the event.This is an absolute no go for GUI programming. Use a timer instead.
Here is an example on how a timer can be used to periodically call a method. In your case to perform the refresh of the page.