Calling Sub from EventHandler

Posted by madlan on Stack Overflow See other posts from Stack Overflow or by madlan
Published on 2010-06-07T19:47:52Z Indexed on 2010/06/07 19:52 UTC
Read the original article Hit count: 353

Filed under:

I'm using the below to update controls from another thread (works great) How would I call a Sub (Named UpdateList)? The UpdateList updates a listview with a list of databases on a selected SQL instance, requires no arguments.

Private Sub CompleteEventHandler(ByVal sender As Object, ByVal e As Microsoft.SqlServer.Management.Common.ServerMessageEventArgs)
    SetControlPropertyValue(Label8, "text", e.ToString)

    UpdateList()

    MessageBox.Show("Restore Complete")
End Sub

Delegate Sub SetControlValueCallback(ByVal oControl As Control, ByVal propName As String, ByVal propValue As Object)

Private Sub SetControlPropertyValue(ByVal oControl As Control, ByVal propName As String, ByVal propValue As Object)
    If (oControl.InvokeRequired) Then

        Dim d As New SetControlValueCallback(AddressOf SetControlPropertyValue)
        oControl.Invoke(d, New Object() {oControl, propName, propValue})
    Else
        Dim t As Type = oControl.[GetType]()
        Dim props As PropertyInfo() = t.GetProperties()
        For Each p As PropertyInfo In props
            If p.Name.ToUpper() = propName.ToUpper() Then
                p.SetValue(oControl, propValue, Nothing)
            End If
        Next

    End If

End Sub

Based On: http://www.shabdar.org/cross-thread-operation-not-valid.html

© Stack Overflow or respective owner

Related posts about vb.net