c# - 启用具有并发性的 Queue<T>

标签 c# multithreading .net-3.5 queue

我有一个以前的question我已经提供了我的解决方案;但是,我无权访问 ConcurrentQueue<T>因为我在 .Net 3.5 上。我需要 Queue<T>允许并发。我读了这个question如果一个项目在队列中并且线程方法试图使一个项目出队,并且似乎会出现问题。

我现在的任务是确定我是否可以派生我自己的并发 Queue 类。这是我想出的:

public sealed class ConcurrentQueue : Queue<DataTable>
{
    public event EventHandler<TableQueuedEventArgs> TableQueued;
    private ICollection que;

    new public void Enqueue(DataTable Table)
    {
        lock (que.SyncRoot)
        {
            base.Enqueue(Table);
        }

        OnTableQueued(new TableQueuedEventArgs(Dequeue()));
    }

    //  this is where I think I will have a problem...
    new public DataTable Dequeue()
    {
        DataTable table;

        lock (que.SyncRoot)
        {
            table = base.Dequeue();
        }

        return table;
    }

    public void OnTableQueued(TableQueuedEventArgs table)
    {
        EventHandler<TableQueuedEventArgs> handler = TableQueued;

        if (handler != null)
        {
            handler(this, table);
        }
    }
}

因此,当 DataTable 排队时,EventArgs 会将已出队的表传递给事件订阅者。此实现是否会为我提供线程安全的队列?

最佳答案

快速访问我最喜欢的搜索引擎表明我的内存是正确的; you can get the Task Parallel Library even on .NET 3.5 .另见 The PFX team blog post on the subject , 和 Reactive Extensions您下载它以获得所需的 System.Threading.dll

关于c# - 启用具有并发性的 Queue<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4555307/

相关文章:

vb.net - 在 VB.NET 中连接数组

c# - 如何在开放泛型类型中定义构造函数?

java - java进程中的实际线程数是多少?

.net - AspNetCompiler MSBuild 任务中的 VirtualPath - 它是否必须等于最终部署的虚拟路径?

c++ - 我如何知道我需要使用 boost 在 C++ 中使用锁来保护哪些共享变量?

python - Python 中每个线程使用 Numba 并行时间

c# - 我怎样才能更有效地进行int模式检查?

c# - C# 中静态成员的别名?

c# - C# 对象的 32 位哈希函数

c# - 用子类创建泛型