BF2Statistics.GamespyRedirectForm.VerifyDnsCache C# (CSharp) Method

VerifyDnsCache() protected method

This method ping's the gamepsy services and verifies that the Redirect services are working. This method effectivly replaces the Redirector.VerifyDNSCache() method.
protected VerifyDnsCache ( ) : bool
return bool
        protected bool VerifyDnsCache()
        {
            // Set default as success
            bool DiagnosticResult = true;

            // Set the System.Net DNS Cache refresh timeout to 1 millisecond
            ServicePointManager.DnsRefreshTimeout = 1;

            // If using System Hosts, Wait 10 seconds to allow the Windows DNS Client to catch up
            if (SelectedMode == RedirectMode.HostsFile)
            {
                for (int i = 0; i < 10; i++)
                {
                    // Get second count
                    int o = 10 - i;

                    // Make sure form wasn't cancelled
                    if (!IsHandleCreated) return false;

                    // Tell the main form thread to update the second count
                    Invoke((Action)delegate
                    {
                        labelDnsText.Text = "Verifying DNS Cache Settings in " + o + " seconds...";
                    });

                    // Wait one
                    Thread.Sleep(1000);
                }

                // Make sure form wasn't cancelled
                if (!IsHandleCreated) return false;

                // Fix header to display no time
                Invoke((Action)delegate
                {
                    labelDnsText.Text = "Verifying DNS Cache Settings";
                });
            }

            // Clear Old Entries
            Redirector.DnsCacheReport.Entries.Clear();

            // Loop through each service
            for (int i = 0; i < Services.Length; i++)
            {
                // Make sure this service is enabled
                if ((!Bf2webCheckbox.Checked && i == 3) || (!GpcmCheckbox.Checked && i != 3))
                {
                    SetStatus(i, "Skipped", Resources.question_button, "Redirect was not enabled by user");
                    continue;
                }

                // Prepare for next service
                SetStatus(i, "Checking, Please Wait...", Resources.loading);
                Thread.Sleep(300);

                // Ping server to get the IP address in the dns cache
                try
                {
                    IPAddress HostsIp = (i == 3) ? StatsServerAddress : GamespyServerAddress;
                    DnsCacheResult Result = new DnsCacheResult(Services[i], HostsIp);

                    // Update Gamespy Redirector Cache
                    Redirector.DnsCacheReport.AddOrUpdate(Result);

                    // Throw bad result
                    if (Result.IsFaulted)
                    {
                        // No such hosts is known?
                        if (Result.Error.InnerException != null)
                            SetStatus(i, "Error Occured", Resources.error, Result.Error.InnerException.Message);
                        else
                            SetStatus(i, "Error Occured", Resources.error, Result.Error.Message);

                        // Flag error
                        DiagnosticResult = false;
                        continue;
                    }

                    // Verify correct address 
                    if (!Result.GotExpectedResult)
                    {
                        SetStatus(i, Result.ResultAddresses[0].ToString(), Resources.warning, "Address expected: " + HostsIp.ToString());

                        // Flag error
                        DiagnosticResult = false;
                    }
                    else
                        SetStatus(i, HostsIp.ToString(), Resources.check);
                }
                catch (Exception e)
                {
                    // No such hosts is known?
                    if (e.InnerException != null)
                        SetStatus(i, "Error Occured", Resources.error, e.InnerException.Message);
                    else
                        SetStatus(i, "Error Occured", Resources.error, e.Message);

                    // Flag error
                    DiagnosticResult = false;
                }
            }

            return DiagnosticResult;
        }