예제 #1
0
        // ******************************************************************
        /// <summary>
        /// This function dumps to the ouput window formated lines of the content of both collections...
        /// The list which is thread safe and the obs coll that is used as a readonly list.
        /// Its main purpose is to debug to validate that both list contains the same values in the same order.
        /// </summary>
        private void Dump()
        {
            Debug.Print("=============== Start");

            lock (_syncRoot)
            {
                IEnumerator enum1 = List.GetEnumerator();
                IEnumerator enum2 = ObsColl.GetEnumerator();

                enum1.Reset();
                enum2.Reset();

                bool ok1 = enum1.MoveNext();
                bool ok2 = enum2.MoveNext();

                while (ok1 || ok2)
                {
                    Debug.Print(String.Format("{0,20} - {0,-20}", ok1 == true ? enum1.Current : "-", ok2 == true ? enum2.Current : "-"));

                    if (ok1)
                    {
                        ok1 = enum1.MoveNext();
                    }

                    if (ok2)
                    {
                        ok2 = enum2.MoveNext();
                    }
                }

                ((IDisposable)enum1).Dispose();
                ((IDisposable)enum2).Dispose();
            }

            Debug.Print("=============== End");
        }
예제 #2
0
        // ******************************************************************
        private void ProcessQueue()
        {
            // This Method should always be invoked only by the UI thread only.
            if (!this.Dispatcher.CheckAccess())
            {
                throw new Exception("Can't be called from any thread than the dispatcher one");
            }

            NotifyCollectionChangedEventArgs args;

            while (this._uiItemQueue.TryDequeue(out args))
            {
                switch (args.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    int offset = 0;
                    foreach (T item in args.NewItems)
                    {
                        ObsColl.Insert(args.NewStartingIndex + offset, item);
                        offset++;
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    if (args.NewStartingIndex >= 0)
                    {
                        ObsColl.RemoveAt(args.NewStartingIndex);
                    }
                    else
                    {
                        foreach (T item in args.OldItems)
                        {
                            ObsColl.Remove(item);
                        }
                    }
                    break;

                case NotifyCollectionChangedAction.Replace:
                    // Replace is used for the [] operator. 'Insert' raise an 'Add' event.

                    if (args.NewStartingIndex >= 0 && args.OldStartingIndex < 0)
                    {
                        throw new ArgumentException(String.Format("Replace action expect NewStartingIndex and OldStartingIndex as: 0 <= {0} <= {1}, {2} <= 0.", args.NewStartingIndex, ObsColl.Count, args.OldStartingIndex));
                    }

                    IList listOld = args.OldItems as IList;
                    IList listNew = args.NewItems as IList;

                    if (listOld == null || listNew == null)
                    {
                        throw new ArgumentException("Both argument Old and New item should be IList in a replace action.");
                    }

                    ObsColl[args.NewStartingIndex] = (T)listNew[0];
                    break;

                case NotifyCollectionChangedAction.Reset:
                    ObsColl.Clear();
                    break;

                case NotifyCollectionChangedAction.Move:
                    ObsColl.Move(args.OldStartingIndex, args.NewStartingIndex);
                    break;

                default:
                    throw new Exception("Unsupported NotifyCollectionChangedEventArgs.Action");
                }
            }
        }