首页 > 解决方案 > 实时跟踪系统用户界面中的图形绘制延迟(使用 c# winform 应用程序)

问题描述

我正在使用 winform 应用程序 (C#) 开发实时本地化跟踪系统 UI。我在 UI 可视化面板中绘制图形(大约 3 到 4 秒)时几乎没有延迟。我真的不明白为什么会这样?这是因为服务器和客户端之间的通信延迟吗?

客户端以 100 毫秒的速率将信息发送到服务器,并且以该速率客户端将对象位置信息发送到服务器(服务器实现为 winform 应用程序 (C#))。

在服务器上,数据接收线程在 form_load 事件处理程序下启动。数据接收线程从Client接收对象位置信息,并将这些对象位置图形显示在面板Control上。在面板绘制事件处理程序下完成的图形绘制。我使用 UDP 协议进行服务器和客户端之间的通信。

客户端程序和服务器程序都在不同的系统上运行。

谁能告诉我为什么会发生这种延迟?我正在分享我的代码片段

   public partial class Form1 : Form // server program for 3 objects tracking display
   {
    Pen P;
    Rectangle r1, r2;
    Image i1;
    Double t1x, t1y, t2x, t2y, t3x, t3y;
    Boolean draw1 = false;
    Graphics g;

    Stopwatch time = new Stopwatch(); // time computation of receiver thread
    TimeSpan tstart, tstop;

    public Form1()
    {
        InitializeComponent();
        P = new Pen(Color.LightCyan, 3);
        r1 = new Rectangle(0, 0, 410, 300); // SIZE OF THE RECTANGLE STARTING FROM (0,0) OF THE IMAGE
        i1 = new Bitmap("Childr.png");
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        Thread Data_Read1 = new Thread(new ThreadStart(data_read1));
        Data_Read1.Start();

        typeof(Panel).InvokeMember("DoubleBuffered",
        BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
        null, panel1, new object[] { true });
    }
    public void data_read1()
    {
        UdpClient UdpServer = new UdpClient(1001);
        IPEndPoint Sender = new IPEndPoint(IPAddress.Any, 0);

        while (true)
        {
            Byte[] data1 = UdpServer.Receive(ref Sender);
            String line1 = Encoding.ASCII.GetString(data1);
            var part = line1.Split('\t');

            tstart = time.Elapsed;
            time.Start();
            if (Double.Parse(part[0]) == 1)
            {
                t1x = (Double.Parse(part[1]) / 0.0347) + 96;
                t1y = (Double.Parse(part[2]) / 0.0335) + 362;
            }
            if (Double.Parse(part[0]) == 2)
            {
                t2x = (Double.Parse(part[1]) / 0.0347) + 96;
                t2y = (Double.Parse(part[2]) / 0.0335) + 362;
            }
            if (Double.Parse(part[0]) == 3)
            {
                t3x = (Double.Parse(part[1]) / 0.0347) + 96;
                t3y = (Double.Parse(part[2]) / 0.0335) + 362;
            }

            draw1 = true;
            panel1.Invoke(new Action(() => { panel1.Invalidate(); }));

            time.Stop();
            tstop = time.Elapsed;

            double dt = (Math.Round(tstop.TotalMilliseconds, 2) - Math.Round(tstart.TotalMilliseconds, 2));
            listBox1.Invoke(new Action(() => { listBox1.Items.Add(Math.Round(dt, 2)); }));
        }
    }
    private void panel1_paint(object sender, PaintEventArgs e)
    {
        g = e.Graphics;
        if (draw1 == true)
        {
            g.DrawImage(i1, Convert.ToInt32(t1y), Convert.ToInt32(t1x), 18, 21);
            g.DrawImage(i1, Convert.ToInt32(t2y), Convert.ToInt32(t2x), 18, 21);
            g.DrawImage(i1, Convert.ToInt32(t3y), Convert.ToInt32(t3x), 18, 21);
        }
   }
}

标签: c#.netvisual-studio

解决方案


推荐阅读