Reputation: 177
I have an array with multiple IP's in it.
I have a working method to ping to an 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 a backgroundworker (using .Net 3.5) to start the ping. When it is complete I change the GUI of my form. It all works fine when i ping to one IP. But what I want to do is running all my IP's and instantly updating the form after one IP is completed. So i must be able to see the result of the first IP while the others are 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;";
}
}
}
Upvotes: 1
Views: 912
Reputation: 66439
Set backgroundWorkerPingHost.WorkerReportsProgress = True
so you can report progress back to the UI thread.
private void backgroundWorkerPingHost_DoWork(object sender, DoWorkEventArgs e)
{
foreach (...) // a loop, since you said you're doing this multiple times
{
var hostIsPingable = PingHost("www.google.be");
// Each time you get a response, report the result to the UI thread
((BackgroundWorker)sender).ReportProgress(0, hostIsPingable);
}
}
private void backgroundWorkerPingHost_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Notified by the DoWork event. Get the result and do something with it
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 that you only want to do once when the 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;
}
}
Upvotes: 1