我正在将项目添加到 FlowLayoutPanel 中。每个项目都会对一个 IP 地址进行 ping 测试,并发布它是向上还是向下。我遇到的问题:在每个项目都有结果之前,什么都不会发布到 FlowLayoutPanel。我希望每个项目都在完成后发布,而不是等待所有项目都完成。我在想可能有一种方法可以使用线程来做到这一点?我现在还不确定。一些指导会很棒!这是 foreach 循环的样子:
string[] ipList = ipListTextBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
statusFlowPanel.Controls.Clear();
statusFlowPanel.Controls.Add(p1);
foreach (string ip in ipList)
{;
Label ipAddressLabel = new Label();
ipAddressLabel.Text = ip;
Label statusLabel = new Label();
statusLabel.Text = "Status: ";
statusLabel.Location = new Point(20, 10 + x);
PictureBox updownPicBox = new PictureBox();
updownPicBox.Height = 30;
updownPicBox.Width = 30;
updownPicBox.Location = new Point(80, 0);
Ping pingSender = new Ping();
IPAddress address = IPAddress.Parse(ip);
PingReply reply = pingSender.Send(address);
if (reply.Status == IPStatus.Success)
{
updownPicBox.Image = Properties.Resources.up_arrow;
}
else
{
updownPicBox.Image = Properties.Resources.down_arrow;
}
var ipPanel = new Panel();
//Invoke(new Action(() => ));
statusFlowPanel.Controls.Add(ipPanel);
ipPanel.Controls.Add(updownPicBox);
ipPanel.Controls.Add(statusLabel);
ipPanel.Controls.Add(ipAddressLabel);
ipPanel.Height = 40;
x++;
}