I am working on a small C # application that refreshes a web page until some conditions are met. I have βFireβ = start refreshing button and βStop!β. which should stop the operation. My problem is that it takes 2 attempts to click the stop button, not 1. Below is my code:
Updated code with timer. However, I think it might be better to use a timer, I think that it does not update every second after the first 2-3 updates or does not update at all. Is there a flaw in my code that I cannot 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; }
}
source share