Удаление элемента из коллекции через некоторое время - PullRequest
2 голосов
/ 27 марта 2009

Я хочу удалить Notification из ObservableCollection<Notification> через некоторое время. Есть ли лучший способ, чем запуск нового потока ThreadPool для каждого добавленного элемента и Thread.Sleep там?


Окончательный код на основе ответа 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 ]

2 голосов
/ 27 марта 2009

Не будет ли какой-то таймер более подходящим? После этого вы могли бы просто создать один поток, который, если бы осталось больше элементов, возобновил бы таймер и через секунду проверил бы, нужно ли удалять следующее уведомление.


Edit: Поскольку вы находитесь в .net 3.5, я предполагаю, что WPF использует DispatcherTimer. Это автоматически будет использовать правильный поток для запуска метода, который вы передаете, как я понимаю. Вот НЕПРОВЕРЕННЫЙ код, чтобы попробовать:

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();
        }
    }
}
0 голосов
/ 27 марта 2009

Я бы не создавал поток для каждого вставленного объекта. Вместо этого я бы использовал один поток очистки или использовал бы объект таймера. Когда нить просыпается, она может пройти по списку и очистить старые элементы.

Я бы также переопределил оператор индекса и любые другие методы доступа, чтобы запретить очистку элементов, которые еще не были очищены.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...