Two Generic parameters : Generic Parameters « Generics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Generics » Generic ParametersScreenshots 
Two Generic parameters
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

public interface IDocument {
    string Title {
        get;
    }

    string Content {
        get;
    }
}

public class Document : IDocument {
    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.title = title;
        this.content = content;
    }
}

public class ProcessDocuments<T, U>
    where T : IDocument
    where U : IDocumentManager<T> {
    public static void Start(U dm) {
        new Thread(new ThreadStart(new ProcessDocuments<T, U>(dm).Run)).Start();
    }

    protected ProcessDocuments(U dm) {
        documentManager = dm;
    }

    private U documentManager;

    protected void Run() {
        while (true) {
            if (documentManager.IsDocumentAvailable) {
                T doc = documentManager.GetDocument();
                Console.WriteLine("Processing document {0}", doc.Title);
            }
            Thread.Sleep(new Random().Next(20));
        }
    }
}

public interface IDocumentManager<T> {
    void AddDocument(T doc);
    T GetDocument();
    bool IsDocumentAvailable {
        get;
    }
}

public class DocumentManager<T> : IDocumentManager<T> {
    private readonly Queue<T> documentQueue = new Queue<T>();

    public void AddDocument(T doc) {
        lock (this) {
            documentQueue.Enqueue(doc);
        }
    }

    public T GetDocument() {
        T doc = default(T);
        lock (this) {
            doc = documentQueue.Dequeue();
        }
        return doc;
    }

    public bool IsDocumentAvailable {
        get {
            return (documentQueue.Count > 0true false;
        }
    }
}
class Program {

    static void Main(string[] args) {
        DocumentManager<Document> dm = new DocumentManager<Document>();
        ProcessDocuments<Document, DocumentManager<Document>>.Start(dm);
        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));
        }

    }
}

 
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.