My button requires two clicks instead of one

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; } 

}

+4
source share
3 answers

You must really change your code to use a timer. When you use the GUI thread incorrectly and make periodic calls to Application.DoEvents(); , a click can only be processed in this place, that is, you need to enter a loop to process the event.

This is an absolute lack of GUI programming. Use a timer instead.

Here is an example of how a timer can be used to periodically call a method. In your case, refresh the page.

+3
source

It seems to me that this is due to the fact that you are running everything in one thread, so when your code works, interaction with the user interface is possible (= first click), and although the page reloads interaction with the user interface (= second click ) If this is a problem, execute your refresch logic in a separate thread.

Sort of:

 private void FireButtonClick(object sender, EventArgs e) { Thread worker = new Thread(new ThreadStart(delegate() { //your code }); worker.IsBackground = true; //so it does not block the app from being closed worker.Start(); } 

If you are using UI elements in a stream, you also need to use Invoke !

+2
source

I assume that you are stuck in a For loop and cannot stop until this loop completes. That is why, apparently, two clicks are required and it just waits for a stop.

Instead, try using a thread that updates the web page and calls thread.run () on your Run Button and thread.stop () on your Stop button. Since the web page will be updated in a separate stream, it will never be able to interfere with the interaction with the user interface.

0
source

Source: https://habr.com/ru/post/1390632/


All Articles