Beispiel #1
0
        private void btnPing_Click(object sender, EventArgs e)
        {
            int index = 0;
            List<long> results = new List<long>();

            //Hide all data points
            pingResults.Series["ping"].Points.Clear();
            pingResults.Visible = false;
            lbPingFailure.Text = "";
            lbReturnTime.Visible = false;
            lbAverageReturnTime.Visible = false;
            lbMinReturnTime.Visible = false;
            lbMinTime.Visible = false;
            lbMaxReturnTime.Visible = false;
            lbMaxTime.Visible = false;
            lbConnectionStatus.Visible = false;
            lbConnectionStatusLabel.Visible = false;
            lbAuthenticationStatus.Visible = false;
            lbAuthenticationStatusLabel.Visible = false;

            int timeout = 1000;

            do
            {
               
                try
                {
                    //Ping server selected in drop-down box
                    Ping pinger = new Ping();
                    System.Threading.Thread.Sleep(50);
                    PingReply reply = pinger.Send(piServerPicker2.Text.ToString(), timeout);
                    
                    //if successful, and round trip time to array and chart 
                    if (reply.Status == IPStatus.Success) 
                    {
                        long speed = reply.RoundtripTime;
                        pingResults.Visible = true;
                        pingResults.Series["ping"].Points.AddY(speed);
                        results.Add(speed);
                    }
                    else
                    {
                        //If timeout occurs, display message and exit loop
                        pingResults.Visible = false;
                        lbPingFailure.Text = "Ping Timeout!";
                        break;
                    }
                    pinger.Dispose();
                    index++;

                }
                catch (PingException)//Catch any exceptions and continue with loop
                {
                    continue;
                }
                
            } while (index < 20);

            //Make information items visible
            lbAuthenticationStatus.Visible = true;
            lbAuthenticationStatusLabel.Visible = true;
            lbConnectionStatus.Visible = true;
            lbConnectionStatusLabel.Visible = true;

            //Set information item values depending on ping results
            if (lbPingFailure.Text.Equals("Ping Timeout!"))
            {
                lbAuthenticationStatus.Text = " - ";
                lbConnectionStatus.Text = " - ";
            }
            else
            {
                long pingAverage = (long)results.Average();
                lbAverageReturnTime.Text = pingAverage.ToString() + " ms";
                lbReturnTime.Visible = true;
                lbAverageReturnTime.Visible = true;

                long pingMax = (long)results.Max();
                lbMaxReturnTime.Text = pingMax.ToString() + " ms";
                lbMaxReturnTime.Visible = true;
                lbMaxTime.Visible = true;

                long pingMin = (long)results.Min();
                lbMinReturnTime.Text = pingMin.ToString() + " ms";
                lbMinReturnTime.Visible = true;
                lbMinTime.Visible = true;

                PIServers servers = new PIServers();
                PIServer server = servers[piServerPicker2.PIServer.ToString()];

                //Attempt to connect to server
                try
                {
                    //If sucessful and both 'connection' and 'authentication' information items display this success
                    server.Connect();
                    lbAuthenticationStatus.Text = "Authentication successful";
                    lbConnectionStatus.Text = "Connection successful";
                } catch (OSIsoft.AF.PI.PIAuthenticationException)
                {
                    //If authentication exception occurs, display this
                    lbAuthenticationStatus.Text = "Authentication unsuccessful";
                    lbConnectionStatus.Text = " - ";
                } catch (Exception ex)
                {
                    //If any other exception then unsuccessful connection
                    lbAuthenticationStatus.Text = " - ";
                    lbConnectionStatus.Text = "Connection unsuccessful";

                }
                
                
            }

        }