0

I have a List that every X seconds get update. I want to know each X seconds if a new item/s have been added to the List and what are the items.

For example i have in the List:

index 0 : this is a test
Index 1 : this is number one

Then after X seconds a new item have been added:

index 0 : this is a test
Index 1 : this is number one
Index 2 : Im a new item

What i want to do is to check for any new items it can be one item or 20 after X seconds and all this items should be add to the top of the List. So each time the last item will be the first. If only one item was added he will be the first if 20 items have been added then the item 20 will be the first in the List and item 1 of the 20 will be at number 20.

I asked before but now i know more what i need.

And just doing inser(0,...) is not good.

newText.Add(t[i]);
newText.Add(dateTime[i]);
newText.Add(string.Empty);

If im doing newText.Insert(0,t[i]); it dosent show anything later. If im using the Insert property it won't build the List as i wanted it will add the date&time all of them in the end and also the empty lines...

Thats why i didn't use Insert.

EDIT

I forgot to mention that i need to keep the List format:

newText.Add(t[i]);
newText.Add(dateTime[i]);
newText.Add(string.Empty);

When i mean last item i mean last 3 indexs. Each 3 indexs in the List are like one group/block. The List format is like this:

Index 0 text: hello world Index 1 date&time: 22/6/2014 Index 2 space/empty line:

So when i mean to move to the last item to the top of the List i mean that for exmaple index 28 will be at index 0 then index 29 at index 1 and index 30 at index 2. Since in index 28 i have a text index 29 date&time and index 30 empty line.

This is how the List look like:

enter image description here

  • 2
    If you want last added item to be first, then consider to use `Stack` instead of `List` – Sergey Berezovskiy Jun 22 '14 at 18:32
  • You could construct your own List structure that provides Events or overload an existing one. – MrPaulch Jun 22 '14 at 18:32
  • 3
    The BindingList and ObservableCollection both have events to let you know when the list has changed – Ken Tucker Jun 22 '14 at 18:33
  • 1
    if it has to be a list, you could save the result of list.Length to an integer and, after 20 seconds, check if list.Length is still equal to that integer. If not, subtract the integer value from the .Length value. Since a List is guaranteed to be ordered, the newest items will always be last. – Steffen Winkler Jun 22 '14 at 18:35
  • Updated my question sorry. Forgot the most important thing is to keep the List format and when i mean last item i mean last 3 indexs ( item/block/group all connected to each other ). So they all need to move up . Each time a new item(3 indexs) added they need to to be at the top like i explained in the question edited part. – user3756594 Jun 22 '14 at 19:27
  • Like Sergey says you need a Stack instead of List – SkeetJon Jun 22 '14 at 19:28
  • Kindly check these two links. http://stackoverflow.com/questions/4745994/how-can-i-add-to-a-lists-first-position http://www.dotnetperls.com/stack – Tanul Jun 22 '14 at 19:57

1 Answers1

2

how about using ObservableCollection<T>?

http://msdn.microsoft.com/library/ms668604(v=vs.110).aspx

with this you could monitor the list in real time.

what also would be possible, monitoring the CollectionChanged event and collect it in a second list, which you clear every 20 seconds

user287107
  • 9,286
  • 1
  • 31
  • 47