title: Accessing the UI thread to change dependancy properties in WPF
body: <p>I'm trying to update a dependancy property in VB.Net 4.0 inside of an Async callback. I feel like I am doing this correctly but I'm still getting the "The calling thread cannot access this object because a different thread owns it." error. Does someone see a better way of using delegates in VB.Net 4.0?</p>
  
  <pre><code>Private WithEvents myObj as CallingObject
  Private Delegate Sub MyErrorDel(ByVal strError as string)
  
  Public Property ErrorMessage As String
      Get
         Return CStr(GetValue(ErrorMessageProperty))
      End Get
      Set(ByVal value As String)
          SetValue(ErrorMessageProperty, value)
      End Set
  End Property
  
  Private Sub MySub()
      myObj.CallFuncAsync()
  End Sub
  
  Private Sub DisplayError(ByVal strError as String)
      'Set Dependancy Property value Bound to UI Textbox
      ErrorMessage = strError
  End Sub
  
  Private Sub myObj_CallFuncCompleted(Byval sender as Object, ByVal e as CallFuncEventArgs)
      'Call delegate and pass in error string as argument
      Dim delError as MyErrorDel
      delError = New MyErrorDel(AddressOf DisplayError)
      delError("An error occured")
      Me.Dispatcher.Invoke(delError, System.Windows.Threading.DispatcherPriority.Normal, Nothing)
  End Sub
  </code></pre>
  
  <p>Whenever ErrorMessage gets set inside of DisplayError an exception gets thrown, even though I am using the dispatcher to call DisplayError.</p>
  
  <p>If anyone see any issues with the way I am trying to access Dependancy Properties from a async callback I would really appreciate the feedback.</p>
  
  <p>Thanks for the help!</p>
  
  <p>Oh and I'm pretty new at blogging about code issues. Any suggestions about how to better formulate this question would be welcome as well.</p>