Button disable and enable

前端 未结 6 2433
北海茫月
北海茫月 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:35

    You're not doing anything wrong. The problem is that the UI doesn't get updated until the code inside of your event handler method finishes executing. Then, the button is disabled and immediately enabled in rapid sequence.

    That explains why if you forget to reenable the button control at the end of the event handler method, it is still disabled—because you told it to disable the button in the first line of the method.

    This is a classic case of why you should never perform long-running computational tasks inside of an event handler method, because it blocks the UI from being updated. The computation actually needs to happen on a separate thread. But don't try to create the thread manually, and definitely don't try to update your UI from a separate thread. Instead, use the BackgroundWorker component to handle all of this for you automatically. The linked MSDN documentation has a great sample on how to use it.

    Disable the button before starting the BackgroundWorker, and then re-enable it in its Completed event, signaling the completion of your database load.

提交回复
热议问题