Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/windows-phone-7/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 BackgroundWorker中的集合处理_Windows Phone 7_Collections_Backgroundworker_Outofrangeexception - Fatal编程技术网

Windows phone 7 BackgroundWorker中的集合处理

Windows phone 7 BackgroundWorker中的集合处理,windows-phone-7,collections,backgroundworker,outofrangeexception,Windows Phone 7,Collections,Backgroundworker,Outofrangeexception,我试图使连接到ObservaleCollection的ListBox更加高效,因此对于DB查询,我实现了一个BackgroundWorker来完成这项工作。然后,在这个backgroundworker中,我想向UI中添加每70ms3个条目,这样更多条目的UI(比如100条)就不会被阻塞。 代码如下: void updateTMWorker_DoWork(object sender, DoWorkEventArgs e) { var MessagesInDB = f

我试图使连接到ObservaleCollection的ListBox更加高效,因此对于DB查询,我实现了一个BackgroundWorker来完成这项工作。然后,在这个backgroundworker中,我想向UI中添加每70ms3个条目,这样更多条目的UI(比如100条)就不会被阻塞。 代码如下:

    void updateTMWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        var MessagesInDB = from MessageViewModel tm in MessagesDB.Messages
                                  where tm.Type.Equals(_type)
                                  orderby tm.Distance
                                  select tm;

        // Execute the query and place the results into a collection.
        Dispatcher.BeginInvoke(() => { MessagesClass.Instance.Messages = new ObservableCollection<MessageViewModel>(); });

        Collection<MessageViewModel> tempM = new Collection<MessageViewModel>();
        int tempCounter = 0;

        foreach (MessageViewModel mToAdd in MessagesInDB)
        {
            if (MessagesClass.Instance.Messages.IndexOf(mToAdd) == -1)
            {
                tempM.Add(mToAdd);
                tempCounter = tempCounter + 1;
            }
            if (tempCounter % 3 == 0)
            {
                tempCounter = 0;
                Debug.WriteLine("SIZE OF TEMP:" + tempM.Count());
                Dispatcher.BeginInvoke(() =>
                {
                    // add 3  messages at once
                    MessagesClass.Instance.Messages.Add(tempM[0]);
                    MessagesClass.Instance.Messages.Add(tempM[1]);
                    MessagesClass.Instance.Messages.Add(tempM[2]);
                });
                tempM = new Collection<MessageViewModel>();
                Thread.Sleep(70);
            } 
        }
        // finish off the rest
        Dispatcher.BeginInvoke(() =>
        {
            for (int i = 0; i < tempM.Count(); i++)
            {
                MessagesClass.Instance.Messages.Add(tempM[i]);
            }
        });            
    }
行中:MessagesClass.Instance.Messages.Add(tempM[0]);其中代码尝试访问tempM的第一个元素


有什么问题吗?为什么我不能访问tempM元素,尽管集合大小大于0?

您忘记了线程同步。看看你的代码:

            1: Debug.WriteLine("SIZE OF TEMP:" + tempM.Count());
            Dispatcher.BeginInvoke(() =>
            {
                // add 3  messages at once
                3: MessagesClass.Instance.Messages.Add(tempM[0]);
                MessagesClass.Instance.Messages.Add(tempM[1]);
                MessagesClass.Instance.Messages.Add(tempM[2]);
            });
            2: tempM = new Collection<MessageViewModel>();

谢谢这解决了我的问题。不知何故,我需要更加熟悉线程同步。:)
            1: Debug.WriteLine("SIZE OF TEMP:" + tempM.Count());
            Dispatcher.BeginInvoke(() =>
            {
                // add 3  messages at once
                3: MessagesClass.Instance.Messages.Add(tempM[0]);
                MessagesClass.Instance.Messages.Add(tempM[1]);
                MessagesClass.Instance.Messages.Add(tempM[2]);
            });
            2: tempM = new Collection<MessageViewModel>();
            EventWaitHandle Wait = new AutoResetEvent(false);

            Debug.WriteLine("SIZE OF TEMP:" + tempM.Count());
            Dispatcher.BeginInvoke(() =>
            {
                // add 3  messages at once
                MessagesClass.Instance.Messages.Add(tempM[0]);
                MessagesClass.Instance.Messages.Add(tempM[1]);
                MessagesClass.Instance.Messages.Add(tempM[2]);

                Wait.Set();
            });
            // wait while tempM is not in use anymore
            Wait.WaitOne();

            tempM = new Collection<MessageViewModel>();