任务并行库 INotifyPropertyChanged 不抛出异常?

发布于 2024-12-13 13:07:39 字数 915 浏览 0 评论 0原文

我有一个 wpf 项目,我在绑定到文本框的属性上使用 INotifyPropertyChanged。我正在使用任务(TaskParallelLibrary)在不同的线程上更新此值。它已正确更新并且不会引发异常。我认为它会抛出异常,因为它是在后台线程而不是 UI 线程上运行。当然,如果我直接使用 UI 元素,它会抛出异常。那么,INotifyPropertyChanged 绑定机制是否负责自动分派到 UI 线程?

这是我的属性代码。

private string _textProperty = "";
    public string TextProperty
    {
        get
        {
            return _textProperty;
        }
        set
        {
            if (_textProperty != value)
            {
                _textProperty = value;
                NotifyPropertyChanged("TextProperty");
            }
        }
    }

我的任务创建是

var task = new Task(() =>
        {
            TextProperty = "ABCD"; // Works.
            // txtBox.Text = "ABCD"; // Throws an exception.
        });
        task.Start();

,XAML 中的文本框是

I have a wpf project where I am using INotifyPropertyChanged on a property which binds to the textbox. I am updating this value on a different thread using task (TaskParallelLibrary). It is updated properly and does NOT throw an exception. I was thinking it would throw an exception because it is running on a background thread and not UI thread. Ofcourse it is throwing an exception if I directly use the UI element. So, does INotifyPropertyChanged bind mechanism takes care of dispatching to the UI thread automatically?

Here is my code with the property.

private string _textProperty = "";
    public string TextProperty
    {
        get
        {
            return _textProperty;
        }
        set
        {
            if (_textProperty != value)
            {
                _textProperty = value;
                NotifyPropertyChanged("TextProperty");
            }
        }
    }

and my task creation is

var task = new Task(() =>
        {
            TextProperty = "ABCD"; // Works.
            // txtBox.Text = "ABCD"; // Throws an exception.
        });
        task.Start();

and the textbox in XAML is <TextBox Name="txtBox" Text="{Binding TextProperty}"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-12-20 13:07:39

我认为它会抛出异常,因为它是在后台线程而不是 UI 线程上运行。

WPF 允许您在后台线程上设置绑定值。它将为您处理 UI 线程的封送处理。

但请注意,这对于集合的元素不起作用。例如,如果您想要添加到已绑定的 ObservableCollection 中,则必须封送回 UI 线程。有各种解决方法,但是,如果需要的话可以缓解这个问题。请注意,此行为在 WPF 4.5 中发生了变化< /a>,这将简化未来WPF中的多线程开发。

I was thinking it would throw an exception because it is running on a background thread and not UI thread.

WPF allows you to set a bound value on a background thread. It will handle the marshaling to the UI thread for you.

Be aware, however, that this does not work for elements of a collection. If you want to add to an ObservableCollection<T> which is bound, for example, you'll have to marshal back to the UI thread. There are various workarounds, however, which can ease this if required. Note that this behavior changes in WPF 4.5, which will simplify multithreaded development in WPF in the future.

谈下烟灰 2024-12-20 13:07:39

绑定到单个属性不是线程仿射的。您可以毫无问题地执行此操作,绑定将为您完成必要的操作。
但请注意,这仅适用于单个属性绑定。例如,如果您有一个 ObservableCollection,则无法从另一个线程添加或删除项目,即使该集合是通过绑定绑定的!

Binding to single properties is not thread affine. You can do this without problems, the binding will do the necessary for you.
However take care, this is only for single property bindings. If you have for example an ObservableCollection, you can not add or remove items from another thread, even if the collection is bound through binding!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文