2008-05-05

BackgroundWorker

BackgroundWorker Class

Executes an operation on a separate thread.

Sample Code

1.Create event DoWork.

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'Sample how to disable button send before send email method after that enabled button.
DisableSendButton()
' to do SendEmail method
EnabledSendButton()

End Sub

2. Show how to invoke method when use BackgroundWorker control.

Private Sub DisableSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(DisableSendButton))
Else
btnSend.Enabled = False
btnCancelSend.Visible = True
End If
End Sub

Private Sub EnabledSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(EnabledSendButton))
Else
btnSend.Enabled = True
btnCancelSend.Visible = False
End If
End Sub

3.Start BackgroundWorker

BackgroundWorker1.RunWorkerAsync()