I have a program that I use to get some data from my network through SNMP. I use last .NET and SharpSnmp library. I realize a ForEachAsync method like exposed in this post. So I can execute snmp request in parallel (and elaborate the response), creating a Task for each device in my list. It works, but if a device doesn't reply for some reason my program get stuck. So I need to manage some sort of timeout to "kill" the async function exposed by the library. That is the function I'm calling in my foreachAsync:
public static async Task<Tuple<string, List<Variable>, Exception>>
GetAsync(string ip, IEnumerable<string> vars, int timeout = 5000)
{
try
{
IPAddress agentIp;
bool parsed = IPAddress.TryParse(ip, out agentIp);
if (!parsed)
{
foreach (IPAddress address in
Dns.GetHostAddresses(ip).Where(address => address.AddressFamily == AddressFamily.InterNetwork))
{
agentIp = address;
break;
}
if (agentIp == null)
throw new Exception("Impossibile inizializzare la classe CGesSnmp senza un indirizzo IP valido");
}
IPEndPoint receiver = new IPEndPoint(agentIp, LOCAL_PORT);
VersionCode version = VersionCode.V2;
string community = "public";
List<Variable> vList = new List<Variable>();
foreach (string s in vars)
vList.Add(new Variable(new ObjectIdentifier(s)));
// This is the function I want to "stop" or "kill" in some way
List<Variable> result = (List<Variable>)await Messenger.GetAsync(version, receiver, new OctetString(community), vList);
return new Tuple<string, List<Variable>, Exception>(ip, result, null);
}
catch (Exception ex)
{
return new Tuple<string, List<Variable>, Exception>(ip, null, ex);
}
}