【发布时间】:2023-03-27 20:05:01
【问题描述】:
我有方法 PatchUpdates 调用 CheckConnection 方法来检查与远程 pc 的连接,如果为真,那么它将返回到用户界面的第一个方法并做一些其他的事情
我搜索并发现我需要使用线程所以我正在创建新线程 但我的应用程序挂起并停止,什么也没发生
请问我做错了什么?
谢谢
public void PatchUpdates()
{
try
{
foreach (DataGridViewRow OfficeListRow in DGV_OfficeList.Rows)
{
string vIPAddress;
string vSoruceFilePath;
int RowNum;
foreach (DataGridViewRow FileListRow in DGV_FileList.Rows)
{
Thread thrd = new Thread(new System.Threading.ThreadStart(PatchUpdates));
thrd.Start();
vIPAddress = OfficeListRow.Cells[1].Value.ToString();
vSoruceFilePath = FileListRow.Cells[4].Value.ToString();
RowNum = OfficeListRow.Index;
///Check the connection to pc
if (CheckConnection(vIPAddress) == true)
{
//MessageBox.Show(vIPAddress + " Pingable ");
DGV_OfficeList[2, RowNum].Value = "Online";
OfficeListRow.DefaultCellStyle.BackColor = Color.LightGreen;
}
else
{
//MessageBox.Show(vIPAddress + " Not Pingable ");
DGV_OfficeList[2, RowNum].Value = "Offline";
OfficeListRow.DefaultCellStyle.BackColor = Color.LightCyan;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static bool CheckConnection(string IPAddress)
{
bool vPingable = false;
try
{
Ping png = new Ping();
PingReply PngReply = png.Send(IPAddress);
vPingable = PngReply.Status == IPStatus.Success;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return vPingable;
}
【问题讨论】:
-
使用异步 Ping,以便您可以同时执行多个 ping。
-
在
thrd.Start();上下断点运行代码。你打断点多少次?你认为这是为什么?
标签: c# multithreading .net-3.5