Created
March 23, 2015 16:14
-
-
Save arn-ob/e3e19b000cf2531556ff to your computer and use it in GitHub Desktop.
A Simple Document Manager . ( Need Some suggestion and Modification )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace learnFormEvent | |
{ | |
public class Document | |
{ | |
private string title; | |
public string Title | |
{ | |
get | |
{ | |
return title; | |
} | |
} | |
private string content; | |
public string Content | |
{ | |
get | |
{ | |
return content; | |
} | |
} | |
public Document(string title, string content) | |
{ | |
this.content = content; | |
this.title = title; | |
} | |
} | |
public class DocumentManager | |
{ | |
private readonly Queue<Document> documentQueue = new Queue<Document>(); | |
public void addDocument(Document doc) | |
{ | |
lock (this) | |
{ | |
documentQueue.Enqueue(doc); | |
} | |
} | |
public Document GetDocument() | |
{ | |
Document doc = null; | |
lock(this) | |
{ | |
doc = documentQueue.Dequeue(); | |
} | |
return doc; | |
} | |
public bool IsDocumentAvalable | |
{ | |
get | |
{ | |
return documentQueue.Count > 0; | |
} | |
} | |
} | |
public class ProcessDocument | |
{ | |
public static void Start(DocumentManager dm) | |
{ | |
new Thread(new ProcessDocument(dm).Run).Start(); | |
} | |
private DocumentManager documentManager; | |
protected ProcessDocument(DocumentManager dm) | |
{ | |
documentManager = dm; | |
} | |
protected void Run() | |
{ | |
while (true) | |
{ | |
if(documentManager.IsDocumentAvalable){ | |
Document doc = documentManager.GetDocument(); | |
Console.WriteLine("Processing document {0}", doc.Title); | |
} | |
Thread.Sleep(new Random().Next(20)); | |
} | |
} | |
} | |
class QueuesLearnDocumentManager | |
{ | |
static void Main() | |
{ | |
DocumentManager dm = new DocumentManager(); | |
ProcessDocument.Start(dm); | |
//create document and add then to the DocumentManager | |
for (int i = 0; i < 10; 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)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment