.netwpfmultithreading

How to read textbox.Text value from another thread in WPF?


In my WPF form I have a textbox.
When a timer elapses, the content of the textbox needs to be fetched.
The timer elapsed is working in a different thread then the UI.

The question is kinda two-fold:

--EDIT--
I used the Dispatcher, but had a more verbose call then what John had:

originalTextBox.Dispatcher.Invoke(
    DispatcherPriority.Normal, 
    (ThreadStart) delegate{text=originalTextBox.Text;}
);

Wouldn't mind even terser though. Accessing a text property should be utterly basic.


Solution

  • Since the question does not specify C#, i wanted to show a simple solution for VB.NET:

    text = originalTextBox.Dispatcher.Invoke(Function() As String
        Return originalTextBox.Text
      End Function)