我正在使用winform应用程序(C#)开发实时本地化跟踪系统UI。我在UI可视化面板中绘制图形(大约3到4秒)时遇到了一点延迟。我真的不明白为什么会这样?这是因为服务器和客户端之间的通信延迟?
客户端以100毫秒的速率向服务器发送信息,客户端以此速率向服务器发送对象位置信息(服务器实现为winform应用程序(C#))。
在服务器上,数据接收线程在form_load事件处理程序下启动。数据接收线程从客户端接收对象位置信息,并且这些对象位置图形显示在面板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);
}
}
}