如何观察对基础数组的更改? [英] How can I observe changes to an underlying array?

查看:95
本文介绍了如何观察对基础数组的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个包含各种类的旧式库,其中一个类的属性类型为int[].此数组属性最多可以包含数百万个元素.我希望使用WPF和C#可视化此数组.问题是,如果我将该数组绑定到ItemsControl,则不会收到任何更改通知.另一方面,如果包装到ObservableCollection中,那么我将无法观察到基础数组的更改(这些更改的发生是由于库本身,而不是因为我的代码).

有人可以提出解决方案吗?

干杯,
George.

Hello everybody,

I have a legacy library containing various classes, one of which has a property of type int[]. This array property may contain up to millions of elements. I wish to visualize this array using WPF and C#. The problem is that if I bind that array to an ItemsControl then I don''t get any change notifications. On the other hand, if I wrap into an ObservableCollection then I cannot observe changes to the underlying array (these changes happen because of the library itself, not because of my code).

Can anybody suggest a solution?

Cheers,
George.

推荐答案

您可以运行一个线程,该线程仅监视数组中的更改,然后在检测到更改时将数组重新绑定到控件. />
或者,您可以运行一个数组观察程序线程,该线程将数组自动移入可观察的集合中,然后让自然的WPF绑定机制完成其余工作.

或者,您可以通过某种方式将数组中的每个项目链接到可观察集合中的项目,这时您根本不需要数组监视线程.
You could run a thread that simply watches the array for changes, and then re-bind the array to the control when a change is detected.

Or, you could run an array watcher thread that automatically moves the array into an observable colletion , and let the natural WPF binding mechanism do the rest of the work.

Or, you could somehow link each item in the array to an item in an observable collection, at which point you don''t need the array-watching thread at all.


我想可以很安全地假设您无法大概知道哪个数组元素块最有可能发生变化,所以...

您可以通过在多个线程之间划分索引块来实现负载均衡.由于元素的数量是可变的,因此我将设置某种方法来确定每个线程的每个数组索引块的大小,例如,如果您有5000个元素,则可以创建10个线程,并分配一个线程块.每个线程500个元素.这样可以更快地检测到更改.

如果您有超过5万个元素,则可以创建更多线程和/或观看每个线程更大的块.

整理所有这些内容将是一项艰巨的任务,但如果是我的话,我个人将不得不提出解决方案.

如果您不使用.Net 4,那就太糟糕了,因为它支持多CPU计算机上的并行任务,并且它是专门为解决此类问题而构建的.

顺便说一句,这种负载平衡方法仍然需要您以整数ObservableCollection wqrap数组,并从数组中更新这些整数.我可能会像这样构建我的物品:

I suppose it''s pretty safe to assume that you can''t know approximately what block of array elements is most likely to change, so...

You could kind of load-balance by divvying up index blocks among several threads. Since the number of elements is variable, I would setup some way to determine how big each array index block is on a per-thread basis, so for example, if you have 5000 elements, you could create 10 threads, and assign a block of 500 elements to each thread. This would make the change detection somewhat faster.

If you have over 50k elements, you could create more threads and/or watch larger blocks per thread.

It would be a challenging task to marshall all of these things, but I would personally get a real kick out of having to come up with a solution, if it were me.

It''s too bad you''re not using .Net 4, because it supports parallel tasking on a multi-cpu machine, and it''s built just for problems like this.

BTW, this load-balancing approach would still require you to wqrap the array in an ObservableCollection of integers, and update those integers from from the array. I would probably build my items like this:

public class ArrayItem : IObservable
{
    private Array[int] m_array = null;
    private int m_value;
 
    public int Value
    {
        get
        {
            return m_value;
        }
        set
        {
            m_value = value;
            OnPropertyChanged("Value");
        }
    }
 
    public int Index { get; set; }
     
    //--------------------------------------------------------------------
    public void OnPropertyChanged(string propertyName)
    {
        // blah blah
    }
 
    //--------------------------------------------------------------------
    public ArrayItem(Array[] int source, int index)
    {
        m_array = source;
        Index   = index;
        Value   = m_array[Index];
    }

 
    //--------------------------------------------------------------------
    // call this method from the thread when the array value changes
    public UpdateValue()
    {
        Value   = m_array[Index];
    }
}



当然,如果数组是静态的,则可以避免传入数组,并且可以在应用程序中的任何地方访问它.



Of course, you could probably avoid passing in the array if it''s static and you can get to it anywhere in your app.


这篇关于如何观察对基础数组的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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