c# - Ping to multiple IP's using backgroundworker -


i have array multiple ip's in it.

i have working method ping ip:

        public static bool pinghost(string nameoraddress)     {         if ( nameoraddress == null || nameoraddress == string.empty)         {                           return false;         }          bool pingable = false;         ping pinger = new ping();          try         {             pingreply reply = pinger.send(nameoraddress);             pingable = reply.status == ipstatus.success;         }         catch (pingexception ex)         {             return false;         }         return pingable;     } 

i use backgroundworker (using .net 3.5) start ping. when complete change gui of form. works fine when ping 1 ip. want running ip's , instantly updating form after 1 ip completed. must able see result of first ip while others still pinging.

private void backgroundworkerpinghost_dowork(object sender, doworkeventargs e)     {         hostispingable = pinghost("www.google.be");     }      private void backgroundworkerpinghost_runworkercompleted(object sender, runworkercompletedeventargs e)     {         if (e.cancelled == true)         {          }         else if (e.error != null)         {             messagebox.show("error:" + e.error.message);             htmlelement htmldiv = webbsppagina.document.getelementbyid("ispingable");             htmldiv.style = "width: 20px; height: 20px; background: red; border: solid black 1px;";         }         else         {             if (hostispingable)             {                 htmlelement htmldiv = webbsppagina.document.getelementbyid("ispingable");                 htmldiv.style = "width: 20px; height: 20px; background: green; border: solid black 1px;";             }             else             {                 htmlelement htmldiv = webbsppagina.document.getelementbyid("ispingable");                 htmldiv.style = "width: 20px; height: 20px; background: red; border: solid black 1px;";             }         }     } 

set backgroundworkerpinghost.workerreportsprogress = true can report progress ui thread.

private void backgroundworkerpinghost_dowork(object sender, doworkeventargs e) {     foreach (...)  // loop, since said you're doing multiple times     {         var hostispingable = pinghost("www.google.be");          // each time response, report result ui thread         ((backgroundworker)sender).reportprogress(0, hostispingable);     } }  private void backgroundworkerpinghost_progresschanged(object sender, progresschangedeventargs e) {     // notified dowork event. result ,     var hostispingable = (bool)e.userstate;      if (hostispingable)     {         htmlelement htmldiv = webbsppagina.document.getelementbyid("ispingable");         htmldiv.style = "width: 20px; height: 20px; background: green; border: solid black 1px;";     }     else     {         htmlelement htmldiv = webbsppagina.document.getelementbyid("ispingable");         htmldiv.style = "width: 20px; height: 20px; background: red; border: solid black 1px;";     } }  private void backgroundworkerpinghost_runworkercompleted(object sender, runworkercompletedeventargs e) {     if (e.cancelled)         return;      // leave stuff in here want once when worker ends     if (e.error != null)     {         messagebox.show("error:" + e.error.message);         htmlelement htmldiv = webbsppagina.document.getelementbyid("ispingable");         htmldiv.style = "width: 20px; height: 20px; background: red; border: solid black 1px;";         return;     } } 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -