A Simple Priority Queue in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QueueSpace
{
    public class PriorityQueue<TValue> : PriorityQueue<TValue, int> { }

    public class PriorityQueue<TValue, TPriority> where TPriority : IComparable
    {
        private SortedDictionary<TPriority, Queue<TValue>> dict = new SortedDictionary<TPriority, Queue<TValue>>();

        public int Count { get; private set; }
        public bool Empty { get { return Count == 0; } }

        public void Enqueue(TValue val)
        {
            Enqueue(val, default(TPriority));
        }

        public void Enqueue(TValue val, TPriority pri)
        {
            ++Count;
            if (!dict.ContainsKey(pri)) dict[pri] = new Queue<TValue>();
            dict[pri].Enqueue(val);
        }

        public TValue Dequeue()
        {
            --Count;
            var item = dict.Last();
            if (item.Value.Count == 1) dict.Remove(item.Key);
            return item.Value.Dequeue();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var pq = new PriorityQueue<int>();

            for (int i = 0; i < 10; ++i)
            {
                pq.Enqueue(i,i/2);
            }
            while (!pq.Empty)
            {
                Console.Write(pq.Dequeue() + " ");
            }

            Console.ReadKey();
        }
    }
}

Output

8 9 6 7 4 5 2 3 0 1

Items with the same priority are dequeued in the same order that they were inserted.

.NET Dock Panel

Apparently .NET does not come with any dock widget, like the ones used for the Toolbox and Properties window in Visual Studio. However, there is a freely available one on sourceforge called DockPanel Suite. Unfortunately, it seems to lack any sort of documentation!

This little code snippet below should be enough to get you started though. Just make sure you your form’s IsMdiContainer property to True first.

public Form1() {
    InitializeComponent();

    glControl.Load += new System.EventHandler(glControl_Load);
    glControl.Paint += new System.Windows.Forms.PaintEventHandler(glControl_Paint);
    glControl.Dock = DockStyle.Fill;

    var dockPanel = new DockPanel();
    dockPanel.Dock = DockStyle.Fill;
    Controls.Add(dockPanel);
    dockPanel.BringToFront();

    var mainDock = new DockContent();
    mainDock.ShowHint = DockState.Document;
    mainDock.TabText = "untitled1";
    mainDock.Controls.Add(glControl);
    mainDock.Show(dockPanel);

    var propGrid = new PropertyGrid();
    propGrid.Dock = DockStyle.Fill;

    var propertyDock = new DockContent();
    propertyDock.Controls.Add(propGrid);
    propertyDock.ShowHint = DockState.DockRight;
    propertyDock.TabText = "Properties";
    propertyDock.Show(dockPanel);
}

C# Blocking Queue

If you’re writing a threaded application in C# and you need to wait until a resource becomes available, you can use this class. Very handy for producer/consumer scenarios.

public class BlockingQueue<T>
{
    Queue<T> que = new Queue<T>();
    Semaphore sem = new Semaphore(0, Int32.MaxValue);

    public void Enqueue(T item)
    {
        lock (que)
        {
            que.Enqueue(item);
        }

        sem.Release();
    }

    public T Dequeue()
    {
        sem.WaitOne();

        lock (que)
        {
            return que.Dequeue();
        }
    }
}