0

There is a search button on the form I created. Press the Search button to perform the search under the specified conditions. Depending on the search conditions, the search may take a long time.

In this case, the foam thread stops temporarily. It doesn't matter because the time is not long. However, if the user continues to click the button while the form is still stationary, the search repeats as many times as the button was pressed after the search ends.

I want to prevent the click event when the user is searching by pressing the search button.

private void btSearch_Click(object sender, EventArgs e)
{
    // check disabled button
    if( !btSearch.Enabled )
        return;
    // disable button
    btSearch.Enabled = false;

    // long time searching...
    // long time searching...
    // long time searching...

    // enable button
    btSearch.Enabled = true;
}

I made the above code because I thought I could prevent the button from being pressed again by disabling it when I started searching. However, after the scan continues to complete, the scan is performed again as many times as you click during the scan.

Is there any way to prevent the event that the user clicked on while the UI thread is temporarily stopped?

UPDATE

I attached a video of the example program to clearly show my problem.

video

eloiz
  • 81
  • 9
  • i would think, disabling button would stop it from clicking. You can also add a message box - "Do you want to run search again?" – T.S. Nov 22 '22 at 04:12
  • Perhaps you enable the button somewhere else in your code. The code you use must work well. – Rezaeimh7 Nov 22 '22 at 04:35
  • 1
    you need to refresh the ui after setting it disabled - see https://stackoverflow.com/questions/1360944/force-gui-update-from-ui-thread – pm100 Nov 22 '22 at 05:06
  • 1
    Does this answer your question? [Force GUI update from UI Thread](https://stackoverflow.com/questions/1360944/force-gui-update-from-ui-thread) – pm100 Nov 22 '22 at 05:07
  • @pm100 That's not the answer i want. What I want is to prevent the user from pressing the button several times. The button's Refresh() is just a redraw of the button and does not prevent the event from occurring. I made a video of my problem and updated it for an accurate explanation. – eloiz Nov 22 '22 at 05:26
  • call the ui refresh immediately after you set disabled. It will disbale the control before the search starts – pm100 Nov 22 '22 at 05:29
  • @pm100 I've already tried it and I still see the same problem. – eloiz Nov 22 '22 at 05:30
  • I found the answer to a similar problem to mine through a search. [How do I prevent any button click events from queuing until the event handle is finished with the first call](https://stackoverflow.com/questions/30894162/how-do-i-prevent-any-button-click-events-from-queuing-until-the-event-handle-is?rq=1) – eloiz Nov 22 '22 at 06:51
  • Are you solving the right problem? If the user has decided that a different search is what they want to see, your solution requires them to wait for the wrong results to return before they can do what they want. I'd strongly suggest you look at ways of *cancelling* an existing search if a new search request is submitted. Your users will likely thank you more for that. – Damien_The_Unbeliever Nov 22 '22 at 07:59
  • @Damien_The_Unbeliever Yes, you are right. In general, users do not want to wait because they want to cancel and rescan if they have performed an incorrect scan. I am still in the initial implementation of the function, and I aim to implement the search function rather than user convenience. – eloiz Nov 22 '22 at 08:02
  • It is a good idea to invest the implementation time and run your search on a background thread. That way you don't run into GUI Invalidation problems – demoncrate Nov 22 '22 at 13:54

0 Answers0