'------------------------------------------------------------------------------
'the Main form
'------------------------------------------------------------------------------
Class MainForm
'left out code generated by the Windows Form Designer
Dim ds As DataSource
'---------------------------------------------------------------------------
'handler for the Load event of the MainForm -- create a new data source
'object
'---------------------------------------------------------------------------
Private Sub MainForm_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
ds = New DataSource()
End Sub
'---------------------------------------------------------------------------
'handler for the Click event of the btnUpdateDb button -- start the update
'process
'---------------------------------------------------------------------------
Private Sub btnUpdateDb_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
'show the update form here
Dim dlgUpdateStatus As New UpdateStatusDialog
dlgUpdateStatus.ShowDialog(Me)
'starts the database update in an asychronous process (secondary thread)
ds.BeginUpdateDb()
End Sub
End Class
'------------------------------------------------------------------------------
'the form used to display the database update status
'------------------------------------------------------------------------------
Class UpdateStatusDialog
'left out code generated by the Windows Form Designer
Delegate Sub UpdateStatusHandler(ByVal status As String)
'--------------------------------------------------------------------------
'handles the Load event of the UpdateStatusDialog -- add the event
'handler for the StatusChanged event
'--------------------------------------------------------------------------
Private Sub UpdateStatusDialog_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler ds.StatusChanged, AddressOf OnStatusChanged
End Sub
'--------------------------------------------------------------------------
'handler for the StatusChanged event
'--------------------------------------------------------------------------
Private Sub OnStatusChanged(ByVal status As String)
'update your status form here, but you have to make
'sure to switch to the main UI thread before updating
'the form. NEVER update anything on the UI thread
'from a secondary thread...you'll just end up with
'extremely difficult troubleshooting
If Me.InvokeRequired = True Then
'we are not on the UI thread
Dim handler As UpdateStatusHandler = AddressOf OnStatusChanged
Dim args() As Object = {status}
'switches to the UI thread
Me.BeginInvoke(handler, args)
Return
End If
'if we made it this far, we are now on the UI thread
'go ahead and make changes to the UI
lblStatus.Text = status
End Sub
'--------------------------------------------------------------------------
'handles the Closing event of the UpdateStatusDialog -- remove the event
'handler for the StatusChanged event
'--------------------------------------------------------------------------
Private Sub UpdateStatusDialog_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
RemoveHandler ds.StatusChanged, AddressOf OnStatusChanged
End Sub
End Class
'------------------------------------------------------------------------------
'the object that talks to the database
'------------------------------------------------------------------------------
Class DataSource
Private UpdateDbCallbackHandler As AsyncCallback = AddressOf UpdateDbCallback
Public Event StatusChanged(ByVal status As String)
Private deleg As InvokeUpdateDb
Private ar As IAsyncResult
'--------------------------------------------------------------------------
'start update the DB in an asynchronous process
'--------------------------------------------------------------------------
Public Sub BeginUpdateDb()
deleg = New InvokeUpdateDb(AddressOf UpdateDb)
ar = deleg.BeginInvoke(UpdateDbCallbackHandler, Nothing)
End Sub
'--------------------------------------------------------------------------
'update the DB (can call this directly instead of through BeginUpdateDb to
'update the database in a synchronous process)
'--------------------------------------------------------------------------
Public Sub UpdateDb()
'update the database here. When this is finished,
'the UpdateDbCallback routine will be called
End Sub
'--------------------------------------------------------------------------
'end the update database asynchronous process
'--------------------------------------------------------------------------
Public Sub EndUpdateDb()
If Not deleg Is Nothing Then
deleg.EndInvoke(ar)
End If
End Sub
'--------------------------------------------------------------------------
'Callback routine for UpdateDb asynchronous process
'--------------------------------------------------------------------------
Private Sub UpdateDbCallback(ByVal ar As IAsyncResult)
'place code that you want performed when the update
'is finished here
EndUpdateDb()
End Sub
End Class