我正在開發一個管理網絡中設備的應用程序,在應用程序中的某個點,我必須ping(實際上它不是ping,它是一個SNMP get)所有計算機網絡來檢查它的類型是否屬於我的受管設備。在TPL網絡中Ping所有計算機
我的問題是,ping網絡中的所有計算機是非常慢的(特別是因爲他們中的大多數不會響應我的消息,並會簡單地超時),並且必須異步完成。
我試圖用TLP用下面的代碼來做到這一點:
public static void FindDevices(Action<IPAddress> callback)
{
//Returns a list of all host names with a net view command
List<string> hosts = FindHosts();
foreach (string host in hosts)
{
Task.Run(() =>
{
CheckDevice(host, callback);
});
}
}
但運行速度很慢,當我暫停執行我檢查線程窗口,看到它只有一個線程PING網絡並因此同步運行任務。
當我使用普通線程時,運行速度要快很多,但任務應該會更好,我想知道爲什麼我的任務不是優化並行性。
**編輯** 意見要求對CheckDevice代碼,所以這裏有雲:
private static void CheckDevice(string host, Action<IPAddress> callback)
{
int commlength, miblength, datatype, datalength, datastart;
string output;
SNMP conn = new SNMP();
IPHostEntry ihe;
try
{
ihe = Dns.Resolve(host);
}
catch (Exception)
{
return;
}
// Send sysLocation SNMP request
byte[] response = conn.get("get", ihe.AddressList[0], "MyDevice", "1.3.6.1.2.1.1.6.0");
if (response[0] != 0xff)
{
// If response, get the community name and MIB lengths
commlength = Convert.ToInt16(response[6]);
miblength = Convert.ToInt16(response[23 + commlength]);
// Extract the MIB data from the SNMP response
datatype = Convert.ToInt16(response[24 + commlength + miblength]);
datalength = Convert.ToInt16(response[25 + commlength + miblength]);
datastart = 26 + commlength + miblength;
output = Encoding.ASCII.GetString(response, datastart, datalength);
if (output.StartsWith("MyDevice"))
{
callback(ihe.AddressList[0]);
}
}
}
這可能取決於CheckDevice實際做了什麼 – Liam
另外一個List不是線程安全的,不應該被使用。 [你應該使用ConcurrentBag](http://stackoverflow.com/questions/5874317/thread-safe-listt-property)。這可能是你問題的根源。 CheckDevice有沒有機會鎖定它? – Liam
發佈「CheckDevice」的代碼 –