INotifyPropertyChanged的有螺纹 [英] INotifyPropertyChanged with threads

查看:143
本文介绍了INotifyPropertyChanged的有螺纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

 的BindingList< T>
 

这势必一个DataGridView。在我班上的一个属性采用长来计算的,所以我带螺纹的操作。经过计算后我提出的OnPropertyChanged()事件来通知该值已准备就绪并网发电。

至少,这是理论。但是,由于OnPropertyChanged方法是从differend线程调用我得到电网的OnRow prePaint方法,一些weired例外。

谁能告诉我怎样脱颖而出OnPropertyChanged事件在主线程被excecuted?我不能使用Form.Invoke,因为MyClass类是不知道它运行在一个WinForms应用程序。

 公共类MyClass的:INotifyPropertyChanged的
{
    公众诠释FastMember {获取;集;}

    私人诠释? slowMember;
    公共SlowMember
    {
        得到
        {
            如果(slowMember.HasValue)
               返回slowMember.Value;
            其他
            {
               线程t =新主题(getSlowMember);
               t.Start();
               返回-1;
            }

        }
    }

   私人无效getSlowMember()
   {
       Thread.sleep代码(1000);
       slowMember = 5;
       OnPropertyChanged(SlowMember);
   }

   公共事件PropertyChangedEventHandler的PropertyChanged;
   私人无效OnPropertyChanged(字符串propertyName的)
   {
        PropertyChangingEventHandler诶= PropertyChanging;
        如果(诶!= NULL)
        {
            诶(这一点,E);
        }
   }

}
 

解决方案

在设计,控制只能通过它创建的线程更新。这就是为什么你得到的例外。

考虑使用<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackgroundWorker只有经过长期持久的操作通过订阅的事件处理程序,以 RunWorkerCompleted 已完成更新成员。

I have a

 BindingList<T>

which is bound to a datagridview. One property in my class takes long to calculate, so I threaded the action. After the calculation I raise the OnPropertyChanged() event to notify the grid that the value is ready.

At least, that's the theory. But since the OnPropertyChanged Method is called from a differend thread I get some weired exceptions in the OnRowPrePaint method of the grid.

Can anybody tell me how I fore the OnPropertyChanged event to be excecuted in the main thread? I can not use Form.Invoke, since the class MyClass is not aware that it runs in a Winforms application.

public class MyClass : INotifyPropertyChanged
{
    public int FastMember {get;set;}

    private int? slowMember;
    public SlowMember
    {
        get
        {
            if (slowMember.HasValue)
               return slowMember.Value;
            else
            {
               Thread t = new Thread(getSlowMember);
               t.Start();
               return -1;
            }

        }
    }

   private void getSlowMember()
   {
       Thread.Sleep(1000);
       slowMember = 5;
       OnPropertyChanged("SlowMember");
   }

   public event PropertyChangedEventHandler PropertyChanged;
   private void OnPropertyChanged(string propertyName)
   {
        PropertyChangingEventHandler eh = PropertyChanging;
        if (eh != null)
        {
            eh(this, e);
        }
   }

}

解决方案

By design, a control can only be updated by the thread it was created in. This is why you are getting exceptions.

Consider using a BackgroundWorker and only update the member after the long lasting operation has completed by subscribing an eventhandler to RunWorkerCompleted.

这篇关于INotifyPropertyChanged的有螺纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆