tags:

views:

25

answers:

2

I have a thread running on a child form, I want to activate a control on the parent form but cannot. It works fine if It's done from the child forms UI thread:

(FormMain.SetControlPropertyValue(FormMain.RBSQL2005, "Checked", True))

but not from a thread running on the child form:

Public Class FormRestoreDB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim t = New Thread(AddressOf UpdateListView1)
    t.Start()
End Sub


Private Sub UpdateListView1()
    'FormMain.SetControlPropertyValue(FormMain.RBSQL2005, "Checked", True)
    FormMain.RBSQL2005.Checked = True
End Sub

End Class


Public Class FormMain

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

Public 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

End Class

What am I doing wrong?

alt text

A: 

You need to use Invoke (http://msdn.microsoft.com/en-us/library/aa334883%28v=VS.71%29.aspx) or BeginInvoke to interact with the parent thread.

SimpleCoder
I tried ivoking with the SetControlPropertyValue - no action and no errors.
madlan
Which works fine if not inside the ChildForms thread but not within.
madlan
A: 
 FormMain.SetControlPropertyValue(FormMain.RBSQL2005, "Checked", True)

This is one of the horrors induced by VB.NET's support for using the type name of a form to refer to the instance of the form. It is an anachronism carried over from VB6 where using the type name of a form was normal.

This falls apart when you use threads because "FormMain" has thread affinity. In other words, it has the <ThreadStatic> attribute. When you use it in a worker thread then you get a new instance of the form, not the one that the user is looking at. This new instance isn't visible because its Show() method was never called. Which is why you don't see your code having any effect. If you call Show() in your code then you'll see it pop up. It is however quite dead, the thread isn't pumping a message loop. Another effect you should have noticed was that InvokeRequired is False, you know that can't be right.

You will have to stop using FormMain. If this code lives inside that class then you can simply use Me. If it doesn't then you'll have to pass the form reference with a property or a constructor argument. The Q&D fix is to use Application.OpenForms(0)

Hans Passant
Thanks Hans, that makes sense. Could you point me towards an example? Say how would I change\update a control on a parent form from a thread running on the child?
madlan
Just don't. Raise an event, just like all Windows Forms controls do when something happened. The parent form can subscribe to it and update its own control(s).
Hans Passant