delphi - 如何操作Collections.TQueue顺序?

标签 delphi queue delphi-xe2

我有一个队列,用于存储要传输的数据包:

TfrmFoo = class(TForm)
    public
        tx_queue: System.Generics.Collections.TObjectQueue<TPacket>;

有时我需要将具有最高优先级的数据包排队。我就是这样做的:

for i := 0 to tx_queue.Count do
    // Shift all queue items one space up the queue.
    tx_queue.FItems[tx_queue.Count - i + 1] :=
        tx_queue.FItems[tx_queue.Count - i];
// Add a packet to the--now vacant--top-most position.
tx_queue.FItems[0] := VipPacket;

不幸的是,FItems 是私有(private)的,所以我无权访问它:

E2361 Cannot access private symbol {System.Generics.Collections}TQueue<Comm.TPacket>.FItems

由于我仍然希望能够使用 stdlib 提供的所有功能,因此如何才能在不滚动自己的 FIFO 队列数据结构的情况下执行我所描述的操作?

更新:拥有 2 个队列是一个解决方案,但我的代码中还有另一个地方需要访问队列项,例如我在哪里打印队列:

DebugMsg('Contents of Tx-Queue:');
for i := 0 to (tx_queue.Count - 1) do
    DebugMsg(tx_queue.FItems[i]);

或者一个迭代队列并计算已排队的特定 ID 的数据包数量的函数。

最佳答案

队列并不意味着可以按照您想要的数据结构方式进行操作。使用TList反而。使用 Add 将内容放在后面,Insert 将内容放在前面,使用 ExtractItem(0) 获取第一个项目。

关于delphi - 如何操作Collections.TQueue顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28150649/

相关文章:

multithreading - 如何在不卡住的情况下读取 blobfield?

delphi - 如何指定外部控制台程序的窗口位置?

delphi - 如何在delphi中调用事件而无需用户干预?

python - 将队列与 tkinter(和线程)一起使用

delphi - 是否可以确定 dbEdit 中的文本是否比可见文本长?

macos - 如何在 Delphi XE2 中向 Mac OS Finder 添加菜单项

class - Delphi : Error 2008 Incompatible types for a class property

delphi - 对 TListView 对象内的项目进行分组?

ruby-on-rails - PG::DependentObjectsStillExist:使用 rspec 时出错

jquery - 为什么我无法在不破坏代码的情况下阻止动画队列堆叠?