我有以下代码应该通过TCP/IP发送一系列值,每个值以某种方式修改变量'x','y'和'z'。当我收到任何回应时,我希望代码显示'x','y'和'z'的当前值,但它没有。我试过添加睡眠(),定时器,甚至使用标签而不是文本框,但没有。有任何想法吗?问候显示不够快的变量值
for (i = 0; i < cant; i++)
{
byte[] data = null;
aux = (Convert.ToInt32(v[i, 3] + 48, CultureInfo.InvariantCulture));
//update variables' values
x = x + Convert.ToInt32(v[i, 0]);
y = y + Convert.ToInt32(v[i, 1]);
z = z + Convert.ToInt32(v[i, 2]);
data = System.BitConverter.GetBytes(aux);
NetworkStream stream = client.GetStream();
// String to store the response ASCII representation.
String responseData = String.Empty;
// Send the message to the connected TcpServer.
stream.Write(data, 0, 1);
// Buffer to store the response bytes.
data = new Byte[256];
while (responseData.Length == 0)
{
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
}
//DISPLAY CURRENT VALUES
textBoxX.Text = x.ToString();
textBoxY.Text = y.ToString();
textBoxZ.Text = z.ToString();
}
这对于Winforms或WPF的工作方式非常重要,在代码停止运行并重新进入消息循环之前,控件不会重新绘制。假设Winforms,你可以添加textBoxX.Update()来强制绘制。实际上并不是很实际,如果你做得对,那么它看起来就像人眼中的模糊。如果重要的是要看到所有的值然后使用ListBox来代替。 – 2012-03-14 16:24:20