在超时后从集合中删除项目

时间:2009-03-27 01:44:56

标签: c# multithreading .net-3.5

我希望在超时后从Notification删除ObservableCollection<Notification>。有没有比为每个添加的项目和Thread.Sleep开始新的ThreadPool线程更好的方法?


基于 Nidonocu 的答案的最终代码:

public class NotificationCollection : ObservableCollection<Notification>
{
    private readonly DispatcherTimer timer;

    public NotificationCollection()
        : this(Application.Current.Dispatcher)
    {
    }

    public NotificationCollection(Dispatcher dispatcher)
    {
        this.timer =
            new DispatcherTimer(DispatcherPriority.DataBind, dispatcher);
        this.timer.Tick += this.TimerOnTick;
    }

    protected override void InsertItem(int index, Notification item)
    {
        base.InsertItem(index, item);
        if (!this.timer.IsEnabled)
        {
            this.StartTimer(item);
        }
    }

    private void StartTimer(Notification item)
    {
        var timeout = item.Timestamp + item.Timeout - DateTime.UtcNow;
        if (timeout < TimeSpan.Zero)
        {
            timeout = TimeSpan.Zero;
        }

        this.timer.Interval = timeout;
        this.timer.Start();
    }

    private void TimerOnTick(object sender, EventArgs e)
    {
        this.timer.Stop();

        this.RemoveAt(0);
        if (this.Count > 0)
        {
            this.StartTimer(this[0]);
        }
    }

2 个答案:

答案 0 :(得分:2)

某种Timer不会更合适吗?然后你可以只有一个线程,如果剩下更多的项目,它将恢复计时器,如果下次通知的时间被删除,则会在一秒后再次检查。


修改 因为你在.net 3.5我假设WPF使用DispatcherTimer。根据我的理解,这将自动使用正确的线程来运行您传递的方法。这是尝试的UNTESTED代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.Windows;

namespace WpfApplication1
{
    public class Notification
    {
        public DateTime TimeStamp { get; set; }
    }

    public class NotificationCollection : ObservableCollection<Notification>
    {
        private readonly TimeSpan timeout;

        private DispatcherTimer timer;

        public NotificationCollection(TimeSpan timeout)
            : this(timeout, Application.Current.Dispatcher) { }

        public NotificationCollection(TimeSpan timeout, Dispatcher dispatch)
        {
            this.timeout = timeout;
            timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, this.Cleanup, dispatch);
        }

        protected override void InsertItem(int index, Notification item)
        {
            base.InsertItem(index, item);
            timer.Start();
        }

        private void Cleanup(object o, EventArgs e)
        {
            timer.Stop();
            // Sanity
            if (this.Count == 0)
                return;

            var deadList = from note in this.Items
                           where note.TimeStamp + this.timeout - DateTime.UtcNow < TimeSpan.Zero
                           select note;
            foreach (var note in deadList)
            {
                this.Remove(note);
            }

            if (this.Count > 0)
                timer.Start();
        }
    }
}

答案 1 :(得分:0)

我不会为每个插入的对象创建一个线程。相反,我会有一个清理线程或使用计时器对象。当线程被唤醒时,它可以遍历列表清理旧项目。

我还会覆盖索引运算符和任何其他访问器方法,以禁止应该清理但尚未清理的项目。