Ich habe eine C#-Klasse in Delphi zu migrieren:
Wie würde man sowas in D7 lösen:
Code:
public class BlockingQueue
{
private bool closing;
private readonly Queue<byte[]> queue = new Queue<byte[]>();
public int Count
{
get
{
lock(queue)
{
return queue.Count;
}
}
}
public BlockingQueue()
{
lock(queue)
{
closing = false;
Monitor.PulseAll(queue);
}
}
public bool Enqueue(byte[] item)
{
lock (queue)
{
if(closing || null == item)
{
return false;
}
queue.Enqueue(item);
if(queue.Count == 1)
{
// wake up any blocked dequeue
Monitor.PulseAll(queue);
}
return true;
}
}
public void Close()
{
lock(queue)
{
if(!closing)
{
closing = true;
queue.Clear();
Monitor.PulseAll(queue);
}
}
}
public bool TryDequeue(out byte[] value, int timeout = Timeout.Infinite)
{
lock(queue)
{
while(queue.Count == 0)
{
if(closing ||
(timeout < Timeout.Infinite) ||
!Monitor.Wait(queue, timeout))
{
value = null;
return false;
}
}
value = (byte[])queue.Dequeue();
return true;
}
}
public void Clear()
{
lock(queue)
{
queue.Clear();
Monitor.Pulse(queue);
}
}
}