Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

c# - INotifyPropertyChanged causes cross-thread error

Here is my scenarion:

I have a GridControl bound to a BindingList. At first what I was doing was creating a worker thread and access the BindingList directly, but this was throwing a "Cross-thread operation detected", so I followed the guide here:

http://www.devexpress.com/Support/Center/p/AK2981.aspx

By cloning the original BindingList into the worker thread and changing that one, I got the desired effect. However, I recently implemeneted the INotifyPropertyChanged into the object that is held into the BindingList, and I started getting the error again.

My guess is that the GridView is still listening to the INotifyPropertyChanged from the object.

How can I fix this?

My class:

public class Proxy : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are manipulating the UI from outside of the UI thread (such as from a worker thread), then you need to rejoin the UI thread. You can do this by calling Invoke on the UI control. You can test if this is required by using InvokeRequired.

The pattern typically used is this:

public void ChangeText(string text)
{
   if(this.InvokeRequired)
   {
      this.Invoke(new Action(() => ChangeText(text)));
   }
   else
   {
      label.Text = text;  
   }
}

In your case the UI is being manipulated as a result of INotifyPropertyChanged, so you need to make sure that either you always modify your entity on the UI thread (using the above technique), or use a generic asynchronous INotifyPropertyChanged helper. This is a wrapper around the item being bound. It uses the above technique to ensure the ChangeProperty event fires on the UI thread.

Here's a very crude example of a proxy for an Entity class. This ensures that the property change event rejoins the UI thread, and keeps the entity itself unmodified. Obviously you'll probably want to implement this more generically using DynamicObject for instance.

public class NotificationHelper : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private readonly ISynchronizeInvoke invokeDelegate;
    private readonly Entity entity;

    public NotificationHelper(ISynchronizeInvoke invokeDelegate, Entity entity)
    {
       this.invokeDelegate = invokeDelegate;
       this.entity = entity;

       entity.PropertyChanged += OnPropertyChanged;
    }

    public string Name
    {
       get { return entity.Name; }
    }

    private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
           if (invokeDelegate.InvokeRequired)
           {
               invokeDelegate.Invoke(new PropertyChangedEventHandler(OnPropertyChanged),
                                     new[] { sender, e });
               return;
           }
           PropertyChanged(this, e);
        }
     }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...