47-c# 新建线程Thread,线程内与操作窗体控件

Thread t3 = new Thread(() =>
            {
                Console.WriteLine("a2....");
                WriteLog("ComListForm.t3.begin..");
                RefreshListFormDb();
            });
            t3.IsBackground = true;
            t3.Start();

RefreshListFormDb()需要更新datagridView:

List list = rv_list.Value;
                        MethodInvoker mi = new MethodInvoker(() =>
                        {
                            WriteLog("ComListForm.RefreshListFormDb....5.");
                            UpdateDataGrid(list);
                        });
                        
                        this.BeginInvoke(mi);

UpdateDataGrid这个方法更新Datagridview的内容:

private void UpdateDataGrid(List list)
        {
            WriteLog("ComListForm.UpdateDataGrid....1.");
            if (list==null || list.Count == 0)
            {
                WriteLog("ComListForm.UpdateDataGrid....2.");
                return;
            }
            WriteLog("ComListForm.UpdateDataGrid....3.");
            this.dataGridView1.Rows.Clear();
            WriteLog("ComListForm.UpdateDataGrid....4.");
            int index = 0;
            foreach(ComPhoneBean bean in list)
            {
                index++;
                this.dataGridView1.Rows.Add(new string[] { "" + index, bean.ComPort, bean.Phone, "" + bean.SignalIntensity });
            }
            WriteLog("ComListForm.UpdateDataGrid....5.");
        }

你可能感兴趣的:(c#)