Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i detect if List<string> was changed ? And what is the last item that was added?

Tags:

c#

.net

I have a timer tick event:

private void timer2_Tick(object sender, EventArgs e)
{
    combindedString = string.Join(Environment.NewLine, ListsExtractions.myList);
    richTextBox1.SelectAll();
    richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
    richTextBox1.Text = combindedString;
}

The timer is set to 50000 and the time is running all the time over and over again. Now the List<string> myList when i'm running my program has, for example, 3 items:

Index 0: Hello world
Index 1: 24/7/2014 21:00
Index 2: http://test.com

After 50 seconds there are two options: either the List not changed or it has changed/longer. If not changed do nothing but if changed get the latest added item. For example, if the list has now been changed to this...

Index 0: This is the latest item added in index 0
Index 1: 24/7/2014 22:00
Index 2: http://www.google.com
Index 3: ""
Index 4: Hello world
Index 5: 24/7/2014 21:00
Index 6: http://test.com

... then I need to do another two actions:

  1. When running the program for the first time, check if the most recent item (the string at Index 0 in this example) contains two words/strings. If it does, then do something, otherwise, do nothing.
    However, if it does contain the words/strings and we do "do something", only do it once after 50 seconds; don't do it again even if the words/string exist again in index 0.

  2. After 50 seconds if the List changed and in Index 0 this words/strings exists, do something and again only once after 50 seconds. If the List didn't change, don't do it again even if the words/string still exist in index 0.

    if (rlines[0].Contains("צבע אדום") || rlines[0].Contains("אזעקה"))
    {
        timer3.Start();            
    }
    

I want to start timer3 only if one of the words/strings exist in index 0.

If nothing has changed after 50 seconds don't start timer3 again.

Only if after 50 seconds or later the List changed and one of the words/strings exist in index 0 again only then start timer 3 again.

like image 952
Sharon Kasis Avatar asked Jul 24 '14 04:07

Sharon Kasis


1 Answers

The Generic List<T> class does not support notifications for list changes.
What you are looking for is ObservableCollection<T>.
It has a CollectionChanged that triggered when the collection is being modified.

You can use it in the following manner:

using System.Collections.ObjectModel;

ObservableCollection<string> myList;

//The cnstructor 
public MyClassname()
{
  this.myList = new ObservableCollection<string>();
  this.myList.CollectionChanged += myList_CollectionChanged;
}

void myList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
        //list changed - an item was added.
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            //Do what ever you want to do when an item is added here...
            //the new items are available in e.NewItems
        }
}
like image 92
Avi Turner Avatar answered Nov 08 '22 20:11

Avi Turner