Replies: 1 comment 2 replies
-
What collection are you using underneath the If you want to be threadsafe then I would stick to assigning a new list and using Here is an example below: Application.Init ();
var w = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
ListView lv = new ListView () { Width = Dim.Fill (), Height = Dim.Fill () };
w.Add (lv);
// this is a Thread that will be accumulating new items
Task.Run (() => {
List<int> newList = new List<int> ();
Random r = new Random ();
DateTime lastRefresh = DateTime.Now;
while(true) {
//50 milliseconds
Task.Delay (50).Wait();
newList.Add (r.Next ());
// Every 5 seconds
if(DateTime.Now - lastRefresh > TimeSpan.FromSeconds(5)) {
lastRefresh = DateTime.Now ;
// take the current state of the list and write it to the ListView but do it on the main UI thread
Application.MainLoop.Invoke (() => {
// maintain the selection / scroll index when updating
var i = lv.SelectedItem;
var top = lv.TopItem;
// create a copy of the list to display in the UI
var copy = newList.ToList ();
lv.SetSource (copy);
lv.SelectedItem = i;
lv.TopItem = top;
});
}
}
}); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Could it happen that I read the SelectedItem value of a ListView and at the same time it's been modified so it creates some kind of issue? What if I want to change SelectedItem not manually but in the code?
Beta Was this translation helpful? Give feedback.
All reactions