惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
G
Google Developers Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
博客园 - 聂微东
V
Visual Studio Blog
博客园_首页
D
DataBreaches.Net
腾讯CDC
I
InfoQ
F
Fortinet All Blogs
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
月光博客
月光博客
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog

博客园 - swordzj

从源码编译PyQt5的过程 安装qt5的运行环境 四舍六入五成双 用批处理在指定目录启动Notebook 华为S6700交换机的命名规则 给服务器来点压力 配置dovect监听多个端口 OpenSSH 9.2P1 aarch64 编译RPM包及升级处理过程 php7.4 注意事项 不重启服务器在线扩容FCSAN存储 WSUS安装后部署报CLR20错误 NFS服务端口转发 注册表模拟修改Wsus配置 Linux下删除大量文件 Seafile社区版集成Office Online Server实现office文件预览 Exchange 域用户无权管理邮箱 阿里云ECS(Centos)开启X11的步骤 微软官方提供的免费正版 Windows 8.1/Win10/7/XP/Vista 操作系统虚拟机镜像下载 在VS2012下静态链接MFC的问题 【转】开源软件是否真的如我们想象中的那样安全?
扩展BindingList,防止增加、删除项时自动更新界面而不出现“...
swordzj · 2014-08-17 · via 博客园 - swordzj

在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于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();
        }
    }