9

これが私のシナリオです:

BindingList にバインドされた GridControl があります。最初はワーカー スレッドを作成して BindingList に直接アクセスしていましたが、これは「クロススレッド操作が検出されました」をスローしていたので、こちらのガイドに従いました。

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

元の BindingList をワーカー スレッドに複製し、それを変更することで、目的の効果が得られました。ただし、最近、BindingList に保持されているオブジェクトに INotifyPropertyChanged を実装したところ、再びエラーが発生し始めました。

私の推測では、GridView はまだオブジェクトから INotifyPropertyChanged をリッスンしています。

どうすればこれを修正できますか?

私のクラス:

public class Proxy : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
4

4 に答える 4

13

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);
        }
     }
 }
于 2012-04-14T20:27:02.350 に答える
1

誰かが同じ問題に遭遇した場合に備えて...私は数時間後にそれを修正することができました。これが私がしたことです:

基本的に問題は、INotifyPropertyChangedを実装するオブジェクトがワーカースレッドに存在していたことでした。これにより、UIスレッドにアクセスするときに問題が発生します。

したがって、私が行ったことは、更新する必要のあるオブジェクトへの参照をINotifyPropertyChangedオブジェクトに渡し、それに対してinvokeを使用することでした。

これがどのように見えるかです:

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            //If the Proxy object is living in a non-UI thread, use invoke
            if (c != null)
            {
                c.BeginInvoke(new Action(() => handler(this, new PropertyChangedEventArgs(name))));
            }
            //Otherwise update directly
            else
            {
                handler(this, new PropertyChangedEventArgs(name));
            }

        }
    }

    //Use this to reference the object on the UI thread when there is need to
    public Control C
    {
        set { c = value; }
    }

スレッドから、私がしたのは:

                    prox.c = this;
                    //Logic here
                    prox.c = null;

これが誰かを助けることを願っています!!

于 2012-04-14T20:45:11.203 に答える