c# - Ping.SendAsyncCancel hangs task -
i'm using ping class ping few hosts periodically. if time next ping comes, previous ping not completed yet, call sendasynccancel terminate it.
problem appears if disable network interface. in case, asyncronous callback never called , call sendasynccancel never returns.
some more info: i'm using .net 3.5 , c# vs2008 express on windows 7 x64. call pings form's timer.tick callback. create ping class once each host (3 hosts in total, same 1 host). timeout 5 seconds. problem 100% repeatable.
all found problem ping crash on multiple create/destroy ping classes, not case.
using system; using system.net.networkinformation; using system.windows.forms; namespace testping { public partial class form1 : form { private ping pinger; public form1() { initializecomponent(); pinger = new ping(); pinger.pingcompleted += new pingcompletedeventhandler(pingcompletedcallback); } private void pingcompletedcallback(object sender, pingcompletedeventargs e) { txtresult.text = e.cancelled ? "cancelled" : e.reply.status.tostring(); } private void butsend_click(object sender, eventargs e) { txtresult.text = "(result)"; txtstatus.text = "sendasync() calling..."; pinger.sendasync(txthost.text, null); txtstatus.text = "sendasync() done."; } private void butcancel_click(object sender, eventargs e) { txtstatus.text = "sendasynccancel() calling..."; pinger.sendasynccancel(); txtstatus.text = "sendasynccancel() done."; } } }
pinger.sendasynccancel(); seem not asynchronously. when using .net 3.5 can following:
private void butsend_click(object sender, eventargs e) { txtstatus.text = "pinging"; pinger.sendasync(txthost, null); } private void butcancel_click(object sender, eventargs e) { thread t = new thread(pinger.sendasynccancel); t.start(); }
now, txtstatus.text = "cancel done"; go here:
private void pingcompletedcallback(object sender, pingcompletedeventargs e) { if(e.cancelled) { txtresult.text = "cancelled"; txtstatus.text = "cancel done"; } else { txtresult.text = e.reply.status.tostring(); txtstatus.text = "sendasync done"; } }
this works expect on end.
Comments
Post a Comment