How to write Text value of TextBox from another thread in WPF I use the delegate

If would you like to write/update a text of textbox control from another thread, I use the delegate in this way.

  
 'call this sub passing name of your textbox control  and string
  UpdateText(NameOfYourTextbox, yourdata)
'it will concatenate yourdata into your textbox control
 


    Private Delegate Sub UpdateTextCallback(control As System.Windows.Controls.TextBox, text As String)

    ' Thread safe updating of control's text property
    Private Sub UpdateText(YourTextbox As System.Windows.Controls.TextBox, text As String)
        If Dispatcher.CheckAccess() Then
            YourTextbox.Text &= text 'concatenate string
        Else
            Dim deleg As New UpdateTextCallback(AddressOf UpdateText)
            Dispatcher.Invoke(deleg, New Object() {YourTextbox, text})
        End If
    End Sub
Advertisements