Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
        }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...