如何从WPF中的另一个线程读取textbox.Text价值? [英] How to read textbox.Text value from another thread in WPF?
本文介绍了如何从WPF中的另一个线程读取textbox.Text价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的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线程交叉线的值(我发现了几个,他们看起来太冗长什么应该是很基本的)?
- 在我不能读取非阻塞的方式的文字?我不关心在这种情况下,线程安全的。
- 编辑 -
我用的调度,但有一个更详细的电话又是什么<一个href="http://stackoverflow.com/questions/710034/how-to-read-textbox-text-value-from-another-thread-in-wpf/710244#710244">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 $ C $,C>
定期在UI线程上执行的消息 - 使用一个
OneWayToSource
绑定连接文本
属性在你的后台组件的属性。这样一来,你就不会做任何工作,以获得属性值 - 它已经被提供给您的组件
- Use the
Dispatcher
to schedule a message to execute on the UI thread from a background thread. ADispatcherPriority
ofSend
will get you the fastest response possible. - Use a
DispatcherTimer
to periodically execute messages on the UI thread. - Use a
OneWayToSource
binding to connect theText
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.
这篇关于如何从WPF中的另一个线程读取textbox.Text价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文