Button disable and enable

前端 未结 6 2304
北海茫月
北海茫月 2021-02-20 16:35

I have a vb.net based windows application, where when \"GO\" button is clicked a bunch of data is loaded into DB. So in my application as soon as \"GO\" button is clicked I want

6条回答
  •  佛祖请我去吃肉
    2021-02-20 17:12

    The button click event is handled as soon as the UI thread has idle time. After you disable your button, the UI thread is keept busy by your code. At the end of your method, you re-enable the button, and after that you exit the method and allow for idle time. As a consequence, the button will already be enabled at the point in time where the click event is handled, so your click is "recognized".

    The solution is, as others already suggested, to use a Backgroundworker.

    Dont try to use doEvents() as a solution (never do), since this would be prone to introduce other subtle problems. That said, you can prove the above explanation with some experimental doEvents in your code. You will see that the click is discarded if a doEvents is performed before the button gets re-enabled. On the other hand, performing a doEvents directly after the button.disable (to "update the GUI") will not help if it is executed before the click.

提交回复
热议问题