vb.net - Exception error trying to ping with a timer -
i've beeen getting error cannot seem solve try...catch , want advice on how solve this. there's more code believe has error i'm getting. code this:
private sub timer1_tick(sender object, e eventargs) handles timer1.tick dim ip string dim sw new stopwatch ip = "some.ip.com" seek if my.computer.network.ping(ip) ping = (sw.elapsedmilliseconds) label1.text = ping & "ms" sw.stop() msgbox("you have no connection.") end if grab ex exception msgbox(ex) end seek if ping < 200 label2.backcolor = color.green label2.text = "good connection" elseif ping > 200 label2.backcolor = color.orange label2.text = "medium connection" elseif ping > 400 label2.backcolor = color.red label2.text = "bad connection" elseif ping <= 0 label2.text = "no connection" label2.backcolor = color.brown end if the error gives me exception error:
************** exception text ************** system.net.networkinformation.pingexception: exception occurred during ping request. ---> system.net.sockets.socketexception: requested name valid, no info of requested type found @ system.net.dns.getaddrinfo(string name) @ system.net.dns.internalgethostbyname(string hostname, boolean includeipv6) @ system.net.dns.gethostaddresses(string hostnameoraddress) @ system.net.networkinformation.ping.send(string hostnameoraddress, int32 timeout, byte[] buffer, pingoptions options) --- end of inner exception stack trace --- @ system.net.networkinformation.ping.send(string hostnameoraddress, int32 timeout, byte[] buffer, pingoptions options) @ system.net.networkinformation.ping.send(string hostnameoraddress, int32 timeout, byte[] buffer) @ microsoft.visualbasic.devices.network.ping(string hostnameoraddress, int32 timeout) @ microsoft.visualbasic.devices.network.ping(string hostnameoraddress) @ sky_casino.form2.timer1_tick(object sender, eventargs e) in c:\users\tonakis2108\documents\visual studio 2013\projects\something\something\form2.vb:line 61 @ system.windows.forms.timer.ontick(eventargs e) @ system.windows.forms.timer.timernativewindow.wndproc(message& m) @ system.windows.forms.nativewindow.callback(intptr hwnd, int32 msg, intptr wparam, intptr lparam)
what want code ping target, homecoming ping in ms via sw , every 5 seconds.
i error when plug connection off test errors while connected program. can help?
the point of try/catch exception still occur, can handle want, instead of programme crashing. next code handle errors more gracefully, , rearranged checks ping time, since combination of less , greater checks isn't going give want, , added longer timeout (the default 500ms). still not greatest code, me handles network beingness disconnected!
private sub timer1_tick(sender object, e eventargs) handles timer1.tick dim ip = "www.google.com" dim timeout = 1000 dim sw = new stopwatch() seek dim ping long = -1 sw.start() if my.computer.network.ping(ip, timeout) sw.stop() ping = sw.elapsedmilliseconds label1.text = string.format("{0}ms", ping) end if if ping < 0 label2.text = "ping timed out" label2.backcolor = color.brown elseif ping < 200 label2.text = "good connection" label2.backcolor = color.green elseif ping < 400 label2.text = "medium connection" label2.backcolor = color.orange else label2.text = "bad connection" label2.backcolor = color.red end if grab ex exception label1.text = "" label2.text = ex.message label2.backcolor = color.red console.writeline(ex.tostring()) end seek end sub vb.net timer
No comments:
Post a Comment