1.//聲明綁定數據,要用ObservableCollection,不能用list,List時單向綁定,ObservableCollection時雙向綁定
private ObservableCollection<string> ListString_Show { get; set; } = new ObservableCollection<string>();
2.//提前綁定號
listBox_Hand_ReceiveComm.ItemsSource = ListString_Show; //顯示數據綁定
3.開始更新
ThreadPool.QueueUserWorkItem((o) =>
{
this.Dispatcher.BeginInvoke(new Action(() =>
{
if (ListString_Show.Count() > 25)
{ ListString_Show.RemoveAt(0); }
//ObservableCollection.Clear();
Num_x += 1;
String NowTime = DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond.ToString();
String Show_One = NowTime + ":" + Str_ShowInfo+"------"+ Num_x.ToString();
ListString_Show.Add(Show_One);
}));
4.測試發送11135個字符串,收到11132個字符串。發送間隔5毫秒。30毫秒以上無丟失。
目前項目要求已經達到,以後再測試更安全的數據收到更新顯示的方法。