控件的.BeginInvoke(new Action(() =>...))语句被执行了两遍?

Gujianda 2021-06-24 12:09:31

一个线程,监视串口接收事件:

        void SenMonitor(object sPort)
        {
            //发送缓冲区空 or 有字符收到 or 接收帧错误
            MyCom.SetComMask(EV_TXEMPTY | EV_RXCHAR | EV_ERR);
            //------------------------------------------------------------
            while (rec_sw)
            {
                uint ComMask = MyCom.WaitComEvent();
                if (ComMask > 0)
                {
                    string tmp = "";
                    tmp = "串口事件" + ComMask.ToString("X2") + "\r\n";
                    txtRec.BeginInvoke(new Action(() => txtRec.Text += tmp));
                    switch (ComMask)
                    {
                        case EV_TXEMPTY:
                        {      break;. }
                        case EV_RXCHAR:
                        {
                            eTime = DateTime.Now.Ticks;
                            allTime = ((Single)(eTime - sTime) / 10).ToString();
                            //---------------------------------
                            uint req = MyCom.GetReceQueu();
                            //---------------------------------
                            if (req == 13)
                            {
                                byte[] r_Buf = new byte[13];
                                int num = MyCom.Read(ref r_Buf, 13);
                                tmp = "读到字符=" + num.ToString() + "\t";
                                for (int i = 0; i < num; i++)
                                    tmp += r_Buf[i].ToString("X2");
                                MyCom.ClearReceiveBuf();
                            }
                            else
                                tmp = "收到字符=" + req.ToString() + "\t";
                            tmp += "\t耗时=" + allTime + "\r\n";
                            //下面这句,莫名其妙执行了两遍!!                           

                            txtRc.BeginInvoke(new Action(() => txtRec.Text += tmp));
                            break;
                        }
                        case EV_ERR:
                        {     break;  }
                    }//switch(ComMask) end
                }//if(ComMask>0) end
            }//While(rec_sw) end
        }

...全文
790 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
IEEE_China 2021-06-24
  • 打赏
  • 举报
回复

代码偷个懒,没看到异常

  public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }


        private async void btnTest_Click(object sender, EventArgs e)
        {
            int n = 1;
            int sleep = 0;
            string s = string.Empty;
            int x = 1;
            while (n < 20)
            {
                x = new Random().Next(1,3) % 2;
                sleep = new Random().Next(100, 1000);
                if (x == 1)
                {
                    s = await TestA(sleep);
                }
                else
                {
                    s = await TestB(sleep);
                }
                OutputMessage(s);
                n++;
            }
            OutputMessage("---- 结束 ----");
        }

        private async Task<string> TestA(int sleep)
        {
            string result = string.Empty;
            await Task.Run(() =>
            {
                result = string.Format($"output:A\tsleep:{sleep}\t---- {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                Thread.Sleep(sleep);
            }
            );
            return result;

        }
        private async Task<string> TestB(int sleep)
        {
            string result = string.Empty;
            await Task.Run(() =>
            {
                result = string.Format($"output:B\tsleep:{sleep}\t---- {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                Thread.Sleep(sleep);
            }
            );
            return result;

        }

        /// <summary>
        /// output
        /// </summary>
        /// <param name="s"></param>
        private void OutputMessage(string s)
        {
            s = string.Format($"{s}\r\n");
            if (txtLog.InvokeRequired)
            {
                txtLog.Invoke(new Action(() => txtLog.AppendText(s)));

            }
            else
            {
                txtLog.AppendText(s);
            }
        }

    }

 

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧