3.C#利用委托解决报错:在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级

1.问题描述

窗体打开时动态绘制控件,后面连接了Socket实时的去刷新panel,重新绘制该区域,发现程序报错,网上查了资料后发现是因为之前创建的控件是由创建UI的线程控制的,后面通信的线程去修改绘制时就会报这个错误。需要委托之前的线程去重新绘制。解决的代码如下:

  /// <summary>
        /// 绘制滚动文本内容.
        /// </summary>
        /// <param name="panelTitle">文字标题的容器.</param>
        /// <param name="panelContent">文字区域的容器.</param>
        private void DrawLabelContent()
        {
            try
            {
                var panelTitles = this.splitContainerMain.Panel2.Controls.Find("panelTitle", true);
                if (panelTitles != null && panelTitles.Length > 0)
                {
                    // 再获取panelContent
                    var panelContents = this.splitContainerMain.Panel2.Controls.Find("panelContent", true);
                    if (panelContents != null && panelContents.Length > 0)
                    {
                        var panelTitle = (Panel)panelTitles[0];
                        var panelContent = (Panel)panelContents[0];

                        panelContent.Controls.Clear(); // 先清空里面的控件
                        if (this.exceptionalEquipmentList != null && this.exceptionalEquipmentList.Count > 0)
                        {
                            for (int i = 0; i < this.exceptionalEquipmentList.Count; i++)
                            {
                                decimal panelRowHeightValue = panelTitle.Height;
                                int panelRowWidth = panelTitle.Width;
                                int panelRowHeight = Convert.ToInt32(panelRowHeightValue);
                                int thisY = panelRowHeight * i;

                                LabelLastY = thisY;
                                // 里面再加一个固定的panel
                                Panel panelRow = new Panel();
                                panelRow.Name = "panelRow" + i;
                                panelRow.Height = Convert.ToInt32(panelRowHeight);
                                panelRow.Width = panelRowWidth;
                                panelRow.BorderStyle = BorderStyle.None;
                                panelRow.BackColor = Color.Transparent;  // 透明
                                panelRow.Location = new Point(0, thisY);  // y就是第几行的y值
                                panelContent.Controls.Add(panelRow);                                                     // contentControl.Controls.Add(panelRow);


                                // 添加标题

                                decimal labeRow1With = panelTitle.Width * FormConst.Row1ItemPanelWidthPont;
                                decimal labeRow2With = panelTitle.Width * FormConst.Row2ItemPanelWidthPont;
                                decimal labeRow3With = panelTitle.Width * FormConst.Row3ItemPanelWidthPont;
                                decimal labeRow4With = panelTitle.Width * FormConst.Row4ItemPanelWidthPont;
                                decimal labeRow5With = panelTitle.Width * FormConst.Row5ItemPanelWidthPont;

                                // 工序
                                Label Label_ProcessesR = new Label();
                                Label_ProcessesR.Height = panelTitle.Height;
                                Label_ProcessesR.Width = Convert.ToInt32(labeRow1With);
                                Label_ProcessesR.Location = new Point(0, 0);
                                Label_ProcessesR.Text = exceptionalEquipmentList[i].procType.ToString();
                                Label_ProcessesR.TextAlign = ContentAlignment.MiddleCenter;

                                // 偏离控制 黄色
                                if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
                                {
                                    Label_ProcessesR.ForeColor = FormConst.LabelColorYellow;
                                }
                                else
                                {
                                    Label_ProcessesR.ForeColor = Color.Red;
                                }

                                panelRow.Controls.Add(Label_ProcessesR);


                                // 设备名称
                                Label Label_EquipmnetNameR = new Label();
                                Label_EquipmnetNameR.Height = panelRowHeight;
                                Label_EquipmnetNameR.Width = Convert.ToInt32(labeRow2With);
                                Label_EquipmnetNameR.Location = new Point(Label_ProcessesR.Width, 0);
                                Label_EquipmnetNameR.Text = exceptionalEquipmentList[i].displayEqpIdShow;
                                Label_EquipmnetNameR.TextAlign = ContentAlignment.MiddleCenter;

                                // 偏离控制 黄色
                                if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
                                {
                                    Label_EquipmnetNameR.ForeColor = FormConst.LabelColorYellow;
                                }
                                else
                                {
                                    Label_EquipmnetNameR.ForeColor = Color.Red;
                                }


                                panelRow.Controls.Add(Label_EquipmnetNameR);

                                // 测试项目
                                Label Label_TestProjectR = new Label();
                                Label_TestProjectR.Height = panelRowHeight;
                                Label_TestProjectR.Width = Convert.ToInt32(labeRow3With);
                                Label_TestProjectR.Location = new Point(Label_EquipmnetNameR.Location.X + Label_EquipmnetNameR.Width, 0);
                                Label_TestProjectR.Text = exceptionalEquipmentList[i].disPlayParamDesc;
                                Label_TestProjectR.TextAlign = ContentAlignment.MiddleCenter;
                                // 偏离控制 黄色
                                if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
                                {
                                    Label_TestProjectR.ForeColor = FormConst.LabelColorYellow;
                                }
                                else
                                {
                                    Label_TestProjectR.ForeColor = Color.Red;
                                }

                                panelRow.Controls.Add(Label_TestProjectR);

                                // 测试结果
                                Label Label_TestResultR = new Label();
                                Label_TestResultR.Height = panelRowHeight;
                                Label_TestResultR.Width = Convert.ToInt32(labeRow4With);
                                Label_TestResultR.Location = new Point(Label_TestProjectR.Location.X + Label_TestProjectR.Width, 0);
                                Label_TestResultR.Text = exceptionalEquipmentList[i].paramValue;
                                Label_TestResultR.TextAlign = ContentAlignment.MiddleCenter;

                                // 偏离控制 黄色
                                if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
                                {
                                    Label_TestResultR.ForeColor = FormConst.LabelColorYellow;
                                }
                                else
                                {
                                    Label_TestResultR.ForeColor = Color.Red;
                                }
                                panelRow.Controls.Add(Label_TestResultR);

                                // 状态
                                Label Label_StateR = new Label();
                                Label_StateR.Height = panelRowHeight;
                                Label_StateR.Width = Convert.ToInt32(labeRow5With);
                                Label_StateR.Location = new Point(Label_TestResultR.Location.X + Label_TestResultR.Width, 0);
                                Label_StateR.Text = exceptionalEquipmentList[i].outRangeDesc;
                                Label_StateR.TextAlign = ContentAlignment.MiddleCenter;

                                // 偏离控制 黄色
                                if (exceptionalEquipmentList[i].outRangeType == "1" || exceptionalEquipmentList[i].outRangeType == "3")
                                {
                                    Label_StateR.ForeColor = FormConst.LabelColorYellow;
                                }
                                else
                                {
                                    Label_StateR.ForeColor = Color.Red;
                                }
                                panelRow.Controls.Add(Label_StateR);

                            }
                        }


                    }
                }
            }
            catch (Exception ex)
            {
                logHelper.SetErrorLog("DrawLabelContent 异常:" + ex.Message);
            }

        }

委托的代码如下:

  // 这边需要用委托因为跨线程了
 this.Invoke(new MethodInvoker(delegate { DrawLabelContent(); }));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码写到35岁

你的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值