Ich habe Probleme mit WaitAll in der unteren Code. Diese Methode empfängt eine Liste von IP-Adressen und einen Port (ich scanne nach WMI-Port 135) und gibt eine Liste von IP-Adressen zurück, bei denen der Port geöffnet ist. Ich glaube, ich habe WaitAll nicht korrekt implementiert, als wenn ich diesen Code verlangsame, scheint es die richtigen Systeme auszuführen und zurückzugeben. Vielen Dank.Threading-Aufgaben zum Scannen öffnen TCP-Port
private static List<IPAddress> openSystems = new List<IPAddress>();
public static List<IPAddress> Scan(List<IPAddress> addresses, ushort port)
{
int count = addresses.Count;
Task[] tasks = new Task[count];
//Loop through ip address
for(int x = 0; x <= count -1; x++)
{
tasks[x] = Task.Factory.StartNew(() =>
{
using (TcpClient tcp = new TcpClient())
{
try
{
Console.WriteLine("Trying to get into {0}", addresses[x]);
tcp.Connect(addresses[x], port);
openSystems.Add(addresses[x]);
}
catch
{
Console.WriteLine("Can't get into {0}", addresses[x]);
//ignore exceptions
}
}
});
}
Task.WaitAll(tasks);
return openSystems;
}
Also, was ist das Problem? Und wie "verlangsamen Sie den Code"? Außerdem benötigen Sie eine 'lock' um' openSystems.Add (Adressen [x]) '' oder verwenden Sie eine der ['concurrent collections'' (https://msdn.microsoft.com/en-us/library/system .collections.concurrent (v = vs.110) .aspx) – Quantic
Ignoriert Exceptions _always_ immer eine schlechte Idee? –