c#多线程以及Dispatcher.Invoke

来源:互联网 发布:网络销售怎么样的 编辑:程序博客网 时间:2024/05/18 17:57

主线程


 private void Stabilitybutton_Click(object sender, RoutedEventArgs e)        {                               for (int Nr = 0; Nr <= 10; Nr++)            {                byte[] msg = new byte[] { 0x00, 0x00, 0x01, 0x01, 0x00, 0xC0, 0x01, 0x16, 0x1B, 0xFF, 0xFF, 0xFF };                msg[4] = (byte)Nr;                               sendMsg(msg);                         }        }




  private void sendMsg(byte[] msgBody, bool isResent = false)        {                      byte[] msg = null;            if (isResent)            {                msg = msgBody;                resendCount++;            }            else            {                resendCount = 0;                msg = msgBody;            }            if (resendCount > 2)            {                LogUtil.Logger.Error("【发送超时且超出最大发送次数】");            }            else            {                currentCmd = msg;                if (ConnectServer())                {                //tcpClient.Send(msg, msg.Length, SocketFlags.None);                //Thread.Sleep(1000);                this.Dispatcher.Invoke(new Action(() => { tcpClient.Send(msg, msg.Length, SocketFlags.None); }));                LogUtil.Logger.Info("【send数据】" + ScaleConvertor.HexBytesToString(msg));                // tcpClient.ReceiveTimeout = 5000;                this.Dispatcher.Invoke(new Action(() => { SendMessageText.AppendText(ReadMessage.Parser.readMessage(msg) + "\n"); }));                 }            }        }

这样的结果是只有等待for循环结束后才执行其他语句,如图:



造成了阻塞



预期结果:


解决方案:

在新的线程中执行发送部分语句,即for循环和发送采用多线程分隔。


失败方案:

由于对Invoke的理解有误,刚开始试图调用Dispatcher.Invoke来进行一个新的线程

 this.Dispatcher.Invoke(new Action(() => { tcpClient.Send(msg, msg.Length, SocketFlags.None); }));
 this.Dispatcher.Invoke(new Action(() => { SendMessageText.AppendText(ReadMessage.Parser.readMessage(msg) + "\n"); }));


在此重新学习一下Dispatcher.Invoke的定义:

从主 UI 线程派生的后台线程不能更新的内容 Button UI 线程上创建。 为了使后台线程访问的内容属性的 Button, ,后台线程必须将工作委托给 Dispatcher 与 UI 线程关联。 这通过使用实现 Invoke 或 BeginInvoke。 Invoke 是同步和BeginInvoke 是异步的。


即 窗体控件是由主线程A执行的,因此在多线程中试图更改控件的内容比如覆盖或追加textbox的内容,是无法在分线程中执行的

//伪代码

 ClientSendThread = new Thread(Send);

 private void Send()
        {
         messagebox.Show("hello");
        }

这样是无法执行的,因为messabox是由UI界面线程控制的,因此采用 this.Dispatcher.Invoke(new Action(() => {}));调用messagebox




回到主题

正确的解决方案:

 ClientSendThread = new Thread(sendMsg);
 ClientSendThread.IsBackground = true;
  ClientSendThread.Start();

0 0
原创粉丝点击