












在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件。但对于数据绑定bindingList而言,没法响应listchang事件,导致后端的grid等控件不能更新数据。废了好大的劲终于找到一个UIBindingList,实现线程数据的同步!
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace TempForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Initial();
}
private UiBindList<int> _list;
private void Initial()
{
_list = new UiBindList<int> { SynchronizationContext = SynchronizationContext.Current };
bindingSource1.DataSource = _list;
new Thread(() =>
{
while (true)
{
Thread.Sleep(1000);
var newItem = DateTime.Now.Second;
_list.Add(newItem);
Thread.Sleep(1000);
_list.Remove(newItem);
}
})
{
IsBackground = true,
}
.Start();
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。