title: Interacting with the UI thread from an Async callback method?
body: <p>I have a method that is asynchronously called when <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.beginread.aspx" rel="nofollow noreferrer">System.Net.Sockets.NetworkStream.BeginRead</a> completes.</p>
  
  <pre><code> skDelegate = New AsyncCallback(AddressOf skDataReceived)
   skStream.BeginRead(skBuffer, 0, 100000, skDelegate, New Object)
  </code></pre>
  
  <p>In that callback method, I need to interact with the UI thread.</p>
  
  <pre><code>Sub skDataReceived(ByVal result As IAsyncResult)
      CType(My.Application.OpenForms.Item("frmMain"), frmMain).refreshStats(d1, d2)
  End Sub
  </code></pre>
  
  <p>This causes an exception after the method completes. (when End Sub is executed)</p>
  
  <blockquote>
    <p>The Undo operation encountered a
    context that is different from what
    was applied in the corresponding Set
    operation. The possible cause is that
    a context was Set on the thread and
    not reverted(undone).</p>
  </blockquote>
  
  <p>So how do I interact with the UI thread from the callback method? What am I doing wrong?</p>