need return values from Threads

Hi at all,

I will "Ping" different Hosts to make sure that they are online.

I start for each host a own thread, because some Pings need more time than other Pings and after one second, all Hosts from my list must be pinged again.
But if I start a ping, I have no control about the results

Here my method.

for
(int i = 0; i < Hosts.lenght; i++)
{
t = new Thread(new ParameterizedThreadStart(Pinger.startPing));
PingParams pp = new PingParams();
pp.Buffer = 32;
pp.Timeout = 12000;
pp.URL = HostsIdea;
t.Start(pp);

}

I think I made a general error, right



Answer this question

need return values from Threads

  • HanJin

    The data needs to be processed from in your startPing thingy already, trough an event or whatever.
    You could use the built-in Ping class too, which supports asynchronous pings, in some way like this:
            void lalala()
            {
                for (int i = 0; i < Hosts.lenght; i++)
                {
                    System.Net.NetworkInformation.Ping _pingthing = new System.Net.NetworkInformation.Ping();
                    _pingthing.PingCompleted += new System.Net.NetworkInformation.PingCompletedEventHandler(_pingthing_PingCompleted);
                    _pingthing.SendAsync(Hosts[i], 12000, new byte[32], null);
                }
            }

            void _pingthing_PingCompleted(object sender, System.Net.NetworkInformation.PingCompletedEventArgs e)
            {
                // e.Reply.RoundtripTime
                // e.Reply.Address
            }


  • need return values from Threads