Beispiel #1
0
 public void AddDocument(Document doc)
 {
     lock (this)
     {
         documentQueue.Enqueue(doc);
     }
 }
        public void AddDocument(Document d)
        {
            Contract.Requires<ArgumentNullException>(d != null, "argument d must not be null");
              //  if (d == null) throw new ArgumentNullException("d");

              AddDocumentToPriorityNode(d, d.Priority);
        }
        private void AddDocumentToPriorityNode(Document doc, int priority)
        {
            Contract.Requires<ArgumentException>(priority >= 0 && priority < 10, "priority value must be between 0 and 9");
              //if (priority > 9 || priority < 0)
              //    throw new ArgumentException("Priority must be between 0 and 9");

              if (priorityNodes[priority].Value == null)
              {
            --priority;
            if (priority >= 0)
            {
              // check for the next lower priority
              AddDocumentToPriorityNode(doc, priority);
            }
            else // now no priority node exists with the same priority or lower
            // add the new document to the end
            {
              documentList.AddLast(doc);
              priorityNodes[doc.Priority] = documentList.Last;
            }
            return;
              }
              else // a priority node exists
              {
            LinkedListNode<Document> prioNode = priorityNodes[priority];
            if (priority == doc.Priority)
            // priority node with the same priority exists
            {
              documentList.AddAfter(prioNode, doc);

              // set the priority node to the last document with the same priority
              priorityNodes[doc.Priority] = prioNode.Next;
            }
            else // only priority node with a lower priority exists
            {
              // get the first node of the lower priority
              LinkedListNode<Document> firstPrioNode = prioNode;

              while (firstPrioNode.Previous != null &&
             firstPrioNode.Previous.Value.Priority == prioNode.Value.Priority)
              {
            firstPrioNode = prioNode.Previous;
            prioNode = firstPrioNode;
              }

              documentList.AddBefore(firstPrioNode, doc);

              // set the priority node to the new value
              priorityNodes[doc.Priority] = firstPrioNode.Previous;
            }
              }
        }
Beispiel #4
0
 static void Main()
 {
     var dm = new DocumentManager();
     ProcessDocuments.Start(dm);
     // Create documents and add them to the DocumentManager
     for (int i = 0; i < 1000; i++)
     {
         Document doc = new Document("Doc " + i.ToString(), "content");
         dm.AddDocument(doc);
         Console.WriteLine("Added document {0}", doc.Title);
         Thread.Sleep(new Random().Next(20));
     }
     Console.ReadKey();
 }
        public void AddDocument(Document d)
        {
            if (d == null) throw new ArgumentNullException("d");

            AddDocumentToPriorityNode(d, d.Priority);
        }