WPF DataGrid性能问题多线程

时间:2014-02-11 21:50:31

标签: c# wpf multithreading c#-4.0 datagrid

我的WPF DataGrid出现了性能问题。 在远程桌面客户端和旧PC上加载数据和滚动时,我的DataGrid非常慢。

我按以下方式加载网格: 首先,我在自己的线程中执行我的sql-thread,以便用户可以在执行语句时与应用程序进行交互。 SQL语句为每一行创建ObservableDicionary<string, string>。创建一行后,我将字典保存在ObservableCollection<ObservableDicionary<string, string>>

while (cDataReader.Read() && GlobalSettings.MainThread.IsAlive && stopThread == false)
{
    ObservableDictionary<string, string> cToAdd = null;
    List<Tuple<string, string>> cTempData = new List<Tuple<string, string>>();

    if (myUseCheckBox)
    {
        cTempData.Add(new Tuple<string, string>("__intern_cv__", "false"));
    }

    for (int i = 0; i < cDataReader.FieldCount; i++)
    {
        string cValue = "";
        string cName = cDataReader.GetName(i).ToLower();

        if (!cDataReader.IsDBNull(i))
        {
            cValue = cDataReader.GetString(i);
        }

        cTempData.Add(new Tuple<string, string>(cName, cValue));
    }

    cToAdd = new ObservableDictionary<string, string>(cTempData.ToDictionary(K => K.Item1, V => V.Item2));

    myGridPaging.RawData.Add(cToAdd);
}

同时,第二个线程从我的ObservableCollection中获取所有ObservableDicionary<string, string>并将其复制到其他名称为 LoadedData BackgoundData 列表的OvservableCollections中:

int cCountRawData = myRawData.Count;

lock ((myGrid as SyGridViewControl).LockObject)
{
    if (cCountRawData > 0)
    {
        List<ObservableDictionary<string, string>> cList = new List<ObservableDictionary<string, string>>(myRawData.GetRange(0, cCountRawData));

        // Add all elements to the grid-paging
        if (myLoadedDatas.Count + myBackgroundDatas.Count <= myPageSize)
        {
            myGrid.Dispatcher.Invoke
                                    (
                                        (Action)delegate()
                                        {
                                            for (int i = 0; i < cCountRawData; i++)
                                            {
                                                Insert(cList[i]);
                                            }
                                        });

        }
        else
        {
            //
            InsertRangeBackgroundList(myRawData.GetRange(0, cCountRawData));
        }

        myDisplayCounterSteps += cCountRawData;
        if (myDisplayCounterSteps >= 100)
        {
            (myGrid as SyGridViewControl).RowCounter = myLoadedDatas.Count + myBackgroundDatas.Count;
            (myGrid as SyGridViewControl).DisplayRowCounter();

            myDisplayCounterSteps = 0;
        }

        // Remove all added elements
        myRawData.RemoveRange(0, cCountRawData);
    }
}

Foreground列表绑定到datagrid。当我在我的数据网格中向下滚动时,我会复制 LoadedDatas 列表中 BackgroundDatas 列表中的项目:

public void LoadNext()
{
    if (myCalledFromLoading)
    {
        myCalledFromLoading = false;
        return;
    }

    if (myBackgroundDatas.Count == 0)
    {
        return;
    }
    else if (myBackgroundDatas.Count <= myPageSize)
    {
        using (ObservableCollectionEx<ObservableDictionary<string, string>> cDelayed = myBackgroundDatas.DelayNotifications())
        {
            foreach (var cObject in cDelayed)
            {
                // Add Object
                myLoadedDatas.Add(cObject);
            }
        }

        // Clear
        myBackgroundDatas.Clear();
    }
    else
    {
        using (ObservableCollectionEx<ObservableDictionary<string, string>> cDelayed = myBackgroundDatas.DelayNotifications())
        {
            for (int i = 0; i < myPageSize; i++)
            {
                // Add Object
                myLoadedDatas.Add(cDelayed[i]);
            }
        }

        // Remove
        for (int i = 0; i < myPageSize; i++)
        {
            myBackgroundDatas.RemoveAt(0);
        }
    }
}

现在我遇到了问题,我的应用程序在本地计算机上非常快,但是当我将我的应用程序部署到终端服务器应用程序时,它的运行速度非常慢。

当我有很多专栏时,问题特别高。今天我已经测试过 183列和10000行。加载所有10000行toke约20秒。 sql语句在一秒钟内执行,所以我发现问题必须是绑定:

var cColumn = new DataGridTextColumn();
cColumn.Binding = new Binding("[" + Config.ColumnName.ToLower() + "]");

也许有人有一个想法,我如何才能提高我的表现......

谢谢!

0 个答案:

没有答案