If you mean the non-generic Queue class, then you can just override Enqueue:public override void Enqueue(object obj){ base.Enqueue(obj); OnChanged(EventArgs.Empty);}但是,如果您指的是通用的 Queue 类,请注意没有合适的虚方法可以覆盖.您最好用自己的类封装队列:However, if you mean the generic Queue<T> class, then note that there is no suitable virtual method to override. You might do better to encapsulate the queue with your own class:(** 重要删除基类!!!**)(** important edit: removed base-class!!! **)class Foo<T>{ private readonly Queue<T> queue = new Queue<T>(); public event EventHandler Changed; protected virtual void OnChanged() { if (Changed != null) Changed(this, EventArgs.Empty); } public virtual void Enqueue(T item) { queue.Enqueue(item); OnChanged(); } public int Count { get { return queue.Count; } } public virtual T Dequeue() { T item = queue.Dequeue(); OnChanged(); return item; }}但是,查看您的代码,您似乎在这里使用了多个线程.如果是这种情况,请考虑使用线程队列.However, looking at your code, it seems possible that you are using multiple threads here. If that is the case, consider a threaded queue instead. 这篇关于C#:将对象添加到队列时触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 08:34