Threadsafe binding with DispatcherObject.CheckAccess()

Posted by maffe on Stack Overflow See other posts from Stack Overflow or by maffe
Published on 2010-04-03T15:21:00Z Indexed on 2010/04/03 15:23 UTC
Read the original article Hit count: 381

Filed under:
|

Hi,

according to this, I can achieve threadsafety with large overhead.
I wrote the following class and use it. It works fine.


public abstract class BindingBase : DispatcherObject, INotifyPropertyChanged,
    INotifyPropertyChanging
{
    private string _displayName;
    private const string NameDisplayName = "DisplayName";

    /// 
    /// The display name for the gui element which bound this instance. It can be used for localization.
    /// 
    public string DisplayName
    {
        get { return _displayName; }
        set
        {
            NotifyPropertyChanging(NameDisplayName);
            _displayName = value;
            NotifyPropertyChanged(NameDisplayName);
        }
    }

    protected BindingBase() {}

    protected BindingBase(string displayName)
    {
        DisplayName = displayName;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangingEventHandler PropertyChanging;

    protected void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged == null)
     return;

        if (CheckAccess())
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
        else
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) (() => NotifyPropertyChanged(name)));
    }

    protected void NotifyPropertyChanging(string name)
    {
        if (PropertyChanging == null)
            return;

        if (CheckAccess())
            PropertyChanging.Invoke(this, new PropertyChangingEventArgs(name));
        else
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) (() => NotifyPropertyChanging(name)));
    }
}

So is there a reason, why I've never found something like that? Are there any issues I should be aware off?

Regards

© Stack Overflow or respective owner

Related posts about wpf-binding

Related posts about multithreading