首页 > 代码库 > WPF 实时绘图的逻辑

WPF 实时绘图的逻辑

实时绘图实际上是两个线程。外部线程直接用thread,只有到绘图那个逻辑才用绘图控件的mycanvas2.Dispatcher.Invoke。

或者说,INVOKE并不是开线程,只是一个绘图的委托而已。

 

主线程:

 

  private void Read_UDP_Click(object sender, RoutedEventArgs e)
        {
            TCPRx mytcp = new TCPRx(" ", );
          

---------------------------------

error

---------------------------------

    //Action<Canvas> updateAction = new Action<Canvas>(mytcp.readdata);
            //updateAction.Invoke(this.FigureShow);
            //this.FigureShow.Dispatcher.Invoke(updateAction,this.FigureShow);
            //Action<Canvas, String> updateAction = new Action<Canvas, string>(mytcp.readdata);
            //this.Dispatcher.BeginInvoke(updateAction, this.FigureShow, "");

            new Thread(() =>
            {
                //this.FigureShow.Dispatcher.Invoke(new Action(() =>
                {
                    while (true)
                    {
                        mytcp.readdata(this.FigureShow);
                        Thread.Sleep(10);
                    }
                }
                //)
                //);
            }).Start();
        }

 

--------------------------------------------------------------------------------------------------------

 绘图

--------------------------------------------------------------------------------------------------------

public void readdata(Canvas mycanvas2)
        {
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint senderRemote = (EndPoint)sender;

            state = new State(myconnect);
            buffer = new byte[length];
            //myconnect.BeginReceiveFrom(buffer, 0, length, SocketFlags.None, ref senderRemote, drawfigure, state);
            //---- myconnect.Receive(buffer);//buffer, 0, length, SocketFlags.None, ref senderRemote, drawfigure, state);

            string message = System.Text.Encoding.Default.GetString(buffer, 0, length);
            string line = System.Text.Encoding.Default.GetString(buffer, 0, length);
      
            if (string.IsNullOrEmpty(line.Trim()))
            {
                return;
            }

            //this.userName.Text = " 测试UI";  
            mycanvas2.Dispatcher.Invoke(
              new Action(
                 delegate()
                 {
                   
                    
                 }
           )
           );


        }

 

WPF 实时绘图的逻辑