1
我對編碼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;
}
* facepalm *你釘了它,Backgroundworker沒有開始,謝謝! –