从后台工作线程(调用)向列表框添加字符串
本文关键字:列表 添加 字符串 调用 后台 工作 线程 | 更新日期: 2023-09-27 18:18:37
我对编码c#相当陌生,并且正在尝试使用后台工作器(以避免停止我的GUI)来运行ping循环,其结果将在我的GUI中的每个ping中打印到列表框,就像标准cmd提示符一样。
我的代码编译,但文本不出现在列表框中,任何帮助,我在哪里出错将是伟大的。
感谢private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int secs = DateTime.Now.Second;
while (keepgoing == true)
{
if (secs != DateTime.Now.Second)
{
secs = DateTime.Now.Second;
using (Ping p = new Ping())
{
int hh = DateTime.Now.Hour;
int mm = DateTime.Now.Minute;
int ss = DateTime.Now.Second;
string time = "";
if (hh < 10)
{
time += "0";
time += hh;
}
else
{
time += hh;
}
time += ":";
if (mm < 10)
{
time += "0";
time += mm;
}
else
{
time += mm;
}
time += ":";
if (ss < 10)
{
time += "0";
time += ss;
}
else
{
time += ss;
}
string successString = ("Ping Successful - " + p.Send(textBox2.Text).RoundtripTime.ToString() + "ms " + time + "'n");
Invoke((Action<string>)AddItemBox2,successString);
}
}
}
listBox2.Items.Add("'n");
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void AddItemBox2(string print)
{
listBox2.Items.Add(print);
listBox2.Refresh();
listBox2.TopIndex = listBox2.Items.Count - 1;
}
你的代码为我工作。正确的列表框?Keepgoing == false?Backgroundworker开始吗?