c# - Background Worker Ping -
problem: when pinging offline device takes 10-15 seconds.
goal: end/kill/stop or ever necessary background worker if ping or backgroundworker runs longer 5 seconds. suggestions how can this?
currently when ping device that's online reply within first second.
private void backgroundworkerping_dowork(object sender, doworkeventargs e) { string pcname = e.argument.tostring(); lblipaddress.invoke((action)(() => lblipaddress.forecolor = color.black)); lblstatus.getcurrentparent().invoke((action)(() => lblstatus.text = string.format("pinging {0}", pcname))); string ipaddress = getsingleping(pcname); e.result = ipaddress; } private string getsingleping(string pcname) { network net = new network(); return net.pinger(pcname); } public class network { //my ping method public string pinger(string pcname, int buffersize = 32) { ping ping = new ping(); byte[] buffer = new byte[buffersize]; pingoptions pingopt = new pingoptions(64, true); try { pingreply pingreply = ping.send(pcname, 100, buffer, pingopt); if (pingreply.status == ipstatus.success) { return pingreply.address.tostring(); } else { return "offline"; } } catch { return "offline"; } } }
edit again:
as stated in previous "edit", have ping timeout set in code. should not have 5...15 second delays operation. maybe problem somewhere else?
for example, take following code (simple console app), iterates list of ips , displays ping results each, in format of ip - state - time used
public class program { private static void main(string[] args) { var p = new program(); p.pingpong(); console.readline(); } private void pingpong() { var ips = new list<string>() { "43.128.240.28", "159.203.123.166", ... 30 in total in test ... "201.131.43.87", "108.232.183.145" }; foreach (var ip in ips) { string ip1 = ip; task.factory.startnew(async () => { var newtwork = new network(); var start = datetime.now; string ping = newtwork.pinger(ip1); console.writeline("{0} - {1} - {2}ms", ip, ping, (datetime.now - start).totalmilliseconds); }); } } } outputs more or less following.
75.146.125.27 - offline - 998.0982ms 190.37.198.208 - offline - 978.329ms 90.82.250.179 - offline - 975.3303ms 141.231.190.96 - offline - 998.3851ms 38.89.231.171 - offline - 976.3265ms 183.179.51.148 - offline - 999.1762ms 125.238.115.199 - offline - 977.1139ms 201.131.43.87 - offline - 975.1229ms 154.165.188.89 - offline - 978.1232ms 86.8.40.161 - offline - 979.1236ms 108.232.183.145 - offline - 998.6617ms none of requests took more 1 (1) second, timeout set myself.
so answer is: launch each request new task , ditch backgroundworker alltogether, setting ping timeout level need, , delegate response ui thread using .begininvoke() if needed (not invoke()).
hope helps forward!
edit:
ok, see set timeout 100ms shouldn't take long. following:
pingreply pingreply; { pingreply = ping.send(pcname, 100, buffer, pingopt); } while(pingreply.status != ipstatus.success); return pingreply.address.tostring(); would rid of loop there?
maybe launch pings separate tasks , let them live long needed?
task.factory.startnew(() => pinger("pcname"));
Comments
Post a Comment