예제 #1
0
        private async void CheckStatus()
        {
            Console.WriteLine("Checking Status...");


            boolPinging = true;

            listMachinesToPing.Clear();


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                {
                    string computer = row.Cells["ColumnComputer"].Value.ToString();

                    listMachinesToPing.Add(computer);
                }
            }

            Console.WriteLine("Count:  {0}", listMachinesToPing.Count);


            if (listMachinesToPing.Count > 0)
            {
                Console.WriteLine("await PingAsync()");

                PingResponse[] arrResponses = null;
                TimeSpan       ts           = new TimeSpan(0, 0, 1);

                arrResponses = await NetworkInformationExtensions.PingAsync(listMachinesToPing.ToArray(), ts, false);


                foreach (PingResponse response in arrResponses)
                {
                    Console.WriteLine("host:  {0}, success?:  {1}", response.HostNameOrAddress, response.IsSuccess);
                    if (response.IsSuccess)
                    {
                        UpdateCellText(response.HostNameOrAddress.ToString(), "ColumnStatus", "Online");
                    }
                    else
                    {
                        string status = "Unavailable";
                        //resolve?
                        IPAddress ip          = null;
                        var       isIpAddress = IPAddress.TryParse(response.HostNameOrAddress, out ip);
                        if (!isIpAddress)
                        {
                            var resolve = DnsResolver.ResolveHostName(response.HostNameOrAddress, TimeSpan.FromMilliseconds(20));
                            if (resolve.IPAddress == null)
                            {
                                status = "Host not found";
                            }
                        }


                        UpdateCellText(response.HostNameOrAddress, "ColumnStatus", status);
                    }
                }
            }


            boolPinging = false;
        }