title: Cannot update control from Class Function that works in a thread
body: <p>Updating the TextBox from a Thread that calls a function within a Class does not work. I'm trying to get this to work in a DLL (but I tried without as well). The DLL is accepting an Object as an argument, like so;</p>
  
  <pre><code>Sub New(ByVal theObject As Object)
  </code></pre>
  
  <p>This allows me to send an object such as this one:</p>
  
  <pre><code>Public Class anObject
      Public Sub doSomething()
          Form1.TextBox1.Text = "test"
      End Sub
  End Class
  </code></pre>
  
  <p>So I will be calling the DLL like this:</p>
  
  <pre><code>Dim theObject As New anObject
  Dim theDLL As New SO_TBFromDLL.UpdateTB(theObject)
  </code></pre>
  
  <p>After which the DLL will start a Thread, that then calls "doSomething"</p>
  
  <pre><code>Public Class UpdateTB
      Dim theObjectSet As Object
  
      Sub New(ByVal theObject As Object)
          theObjectSet = theObject
  
          'Try to update TB from Thread within current class
          Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf bghandler)
          ctThread.Start()
      End Sub
  
      'Try to update TB from Thread within current class
      Sub bghandler()
          theObjectSet.doSomething()
      End Sub
  End Class
  </code></pre>
  
  <p>But this isn't working. I also tried it using a delegate (code below).</p>
  
  <p>I have been trying two other variations in addition to those two;</p>
  
  <p><strong>Try 3 from Thread that's not in Class/DLL.</strong> This works just fine.</p>
  
  <pre><code>Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf receiverFunction)
      ctThread.Start()
  End Sub
  </code></pre>
  
  <p>With <code>receiverFunction</code> being:</p>
  
  <pre><code>'Receiver for Thread that's not in DLL (Button 3)
  Private Sub receiverFunction()
      generaldelegatesN("tb1", "test")
  End Sub
  </code></pre>
  
  <p><strong>Try 4 from Thread that's not in DLL, but use the Function inside of the Class.</strong> This does not work either.</p>
  
  <pre><code>Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
      Dim theObject As New anObjectWithDelegate
      Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf theObject.doSomething)
      ctThread.Start()
  End Sub
  </code></pre>
  
  <p>With <code>anObjectWithDelegate</code> being:</p>
  
  <pre><code>'Unfortunately, with a delegate it still doesn't work (Button2 &amp; Button4)
  Public Class anObjectWithDelegate
      Public Sub doSomething()
          Form1.generaldelegatesN("tb1", "test")
      End Sub
  End Class
  </code></pre>
  
  <p>Why does Try 3 work, but Try 4 doesn't? It's <code>doSomething()</code> function is practically identical to <code>receiverFunction</code> With the exception of the added <code>Form1.</code></p>
  
  <p><strong>Delegation code:</strong> (In case it matters)</p>
  
  <pre><code>'The Delegate functions
  Delegate Sub generaldelegates(ByVal param1 As String, ByVal param2 As String)
  Public generaldelegatesM As generaldelegates
  Public Sub generaldelegatesN(ByVal param1 As String, ByVal param2 As String)
      'Updates control on main thread:
      If Me.InvokeRequired Then
          generaldelegatesM = New generaldelegates(AddressOf generaldelegatesN)
          Me.BeginInvoke(generaldelegatesM, New Object() {param1, param2})
      Else
          Select Case param1
              Case "tb1"
                  TextBox1.Text = param2
          End Select
      End If
  End Sub
  </code></pre>