Beispiel #1
0
        private void PingOne(int index)
        {
            if (mainTable[index].Endpoint == string.Empty)
            {
                var ip = NetTest.GetIP(mainTable[index].HostsName);

                if (ip != null)
                {
                    mainTable[index].Endpoint = ip.AddressFamily == AddressFamily.InterNetwork ? $@"{ip}:{rawTable[index].Port}" : $@"[{ip}]:{rawTable[index].Port}";
                }
                else
                {
                    return;
                }
            }

            var    ipe     = IPFormatter.ToIPEndPoint(mainTable[index].Endpoint);
            double?latency = null;
            var    res     = Timeout;
            var    time    = DateTime.Now;

            try
            {
                latency = NetTest.TCPing(ipe.Address, ipe.Port, Timeout);
            }
            catch
            {
                // ignored
            }

            if (latency != null)
            {
                res = Convert.ToInt32(Math.Round(latency.Value));
            }
            else
            {
                //notifyIcon1.ShowBalloonTip(1000, time.ToString(CultureInfo.CurrentCulture), $"{mainTable[index].HostsName}\n{ipe}", ToolTipIcon.Error);
            }

            var log = new DateTable
            {
                Date    = time,
                Latency = res
            };

            mainTable[index].AddNewLog(log);

            if (MainList.SelectedRows.Count == 1)
            {
                var i = MainList.SelectedRows[0].Cells[0].Value as int?;
                if (i == index && DateList.Visible)
                {
                    DateList.Invoke(() => { LoadLogs(log); });
                }
            }
        }
Beispiel #2
0
        private void FirstPing()
        {
            var t = new Task(() =>
            {
                Parallel.For(0, mainTable.Count, (i, state) =>
                {
                    if (cts_PingTask.IsCancellationRequested)
                    {
                        state.Stop();
                        return;
                    }

                    if (IPFormatter.IsIPAddress(mainTable[i].HostsName))                     //反查DNS
                    {
                        PingOne(i);

                        if (cts_PingTask.IsCancellationRequested)
                        {
                            state.Stop();
                            return;
                        }

                        mainTable[i].HostsName = NetTest.GetHostName(IPAddress.Parse(mainTable[i].HostsName), ReverseDNSTimeout);
                    }
                    else
                    {
                        var ip = NetTest.GetIP(mainTable[i].HostsName);

                        if (cts_PingTask.IsCancellationRequested)
                        {
                            state.Stop();
                            return;
                        }

                        if (ip != null)
                        {
                            mainTable[i].Endpoint = ip.AddressFamily == AddressFamily.InterNetwork ? $@"{ip}:{rawTable[i].Port}" : $@"[{ip}]:{rawTable[i].Port}";
                        }
                        PingOne(i);
                    }

                    if (cts_PingTask.IsCancellationRequested)
                    {
                        state.Stop();
                    }
                });
                //notifyIcon1.ShowBalloonTip(1000, ExeName, @"载入完毕", ToolTipIcon.Info);
            });

            PingTasks.Add(t);
            t.Start();
        }
Beispiel #3
0
        private void FirstPing()
        {
            var t = new Task(() =>
            {
                Parallel.For(0, mainTable.Count, (i, state) =>
                {
                    try
                    {
                        cts_PingTask.Token.ThrowIfCancellationRequested();
                    }
                    catch (OperationCanceledException)
                    {
                        state.Stop();
                        return;
                    }

                    if (IPFormatter.IsIPv4Address(mainTable[i].HostsName))                     //反查DNS
                    {
                        PingOne(i);

                        try
                        {
                            cts_PingTask.Token.ThrowIfCancellationRequested();
                        }
                        catch (OperationCanceledException)
                        {
                            state.Stop();
                            return;
                        }

                        mainTable[i].HostsName = NetTest.GetHostName(IPAddress.Parse(mainTable[i].HostsName), ReverseDNSTimeout);
                    }
                    else
                    {
                        var ip = NetTest.GetIP(mainTable[i].HostsName);

                        try
                        {
                            cts_PingTask.Token.ThrowIfCancellationRequested();
                        }
                        catch (OperationCanceledException)
                        {
                            state.Stop();
                            return;
                        }

                        if (ip != null)
                        {
                            mainTable[i].Endpoint = $@"{ip}:{rawTable[i].Port}";
                        }
                        PingOne(i);
                    }

                    try
                    {
                        cts_PingTask.Token.ThrowIfCancellationRequested();
                    }
                    catch (OperationCanceledException)
                    {
                        state.Stop();
                    }
                });
                //notifyIcon1.ShowBalloonTip(1000, ExeName, @"载入完毕", ToolTipIcon.Info);
            });

            PingTasks.Add(t);
            t.Start();
        }