在我的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:
什么是最简单,最可读的方式来读取一个GUI线程交叉线的值(我发现了几个,他们看起来太冗长什么应该是很基本的)? 在我不能读取非阻塞的方式的文字?我不关心在这种情况下,线程安全的。- 编辑 - 我用的调度,但有一个更详细的电话又是什么John有:
--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.
您可以:
使用调度
安排的消息从后台线程的UI线程上执行。一个 的DispatcherPriority
发送将让你最快的响应可能的。
使用一个 DispatcherTimer 定期在UI线程上执行的消息
使用一个 OneWayToSource
绑定连接文本
属性在你的后台组件的属性。这样一来,你就不会做任何工作,以获得属性值 - 它已经被提供给您的组件
Use the Dispatcher
to schedule a message to execute on the UI thread from a background thread. A DispatcherPriority
of Send
will get you the fastest response possible.
Use a DispatcherTimer
to periodically execute messages on the UI thread.
Use a OneWayToSource
binding to connect the Text
property to a property on your background component. That way, you won't have to do any work to get the property value - it will already have been supplied to your component.