Button disable and enable

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

    Since you're trying to execute a function that can take some time, I'd advise you to make use of threading. In .NET there's a BackgroundWorker component which is excellent for performing tasks asynchronous.

    On button click, invoke the BackgroundWorker like this:

    if not bgwWorker.IsBusy then
       btnGo.enabled = false
       bgwWorker.RunWorkerAsync()
    end if 
    

    And use the completed event to enable the button again:

    Private Sub bgwWorker_DoWork(ByVal sender As System.Object, _
                     ByVal e As System.ComponentModel.DoWorkEventArgs) _
                     Handles bgwWorker.DoWork
    ' Do your things    
    End Sub
    
    Private Sub bgwWorker_RunWorkerCompleted(ByVal sender As System.Object, _
                             ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
                             Handles bgwWorker.RunWorkerCompleted
    ' Called when the BackgroundWorker is completed.
    btnGo.enabled = true
    End Sub
    

    In the example above, I've used bgwWorker as the instance of a BackgroundWorker.

提交回复
热议问题