2

I have programmed an application in Windows Form Application in Visual Studio, and I have already set a timer in my form which will check whether the user has access to the internet or not every 1 second (1000 ms), and if he/she does not have access, a form will pop up and shows that the user does not have access to the internet. This used to work very well until I have found out that the form will be poped out even when the internet shows a timeout, although I wanted to check if the user loses his/her access to the internet completely. I decided to work on another algorithm, and I want the timer to check if the internet has 5 timeouts in a row, and if it does, the form, which shows the user that they do not have access to the internet, pops up. Any specific idea about the mentioned plan? Here is my code in which it shows that the user has access to the internet or not. * LIC is the form which shows that the user does not have access to the internet* Method in the class

public static bool Check_Internet_For_Acceptation()
    {
        if (Connections.InternetConnection("google.com") == false)
        {
            return false;
        }
        else
            return true;
    }

And you can see the code that I have typed in the timer_Tick

private void TimerCheckInternetConnection_Tick(object sender, EventArgs e)
    {
        #region Check whether the users have internet connection or not
        if (Components.Check_Internet_For_Acceptation() == false)
        {

            if (LIC.Visible)
            {
                LIC.Show();
                return;
            }
            else
                LIC.ShowDialog();
            TimerCheckInternetConnection.Enabled = false;
        }
        #endregion
    }

1 Answers1

3

instead of returning true and false, you can return more states:

public enum Status
{
     Unknown,
     Connected,
     NoConnection
}

public static int retries = 0;

public static Status Check_Internet_For_Acceptation()
    {
        if (Connections.InternetConnection("google.com") == false)
        {
            if(retries >=5)
               return Status.NoConnection;
            else
               {retries++; return Status.Unknown;}
        }
        else
            return status.Connected;
    }

and in your timer:

private void TimerCheckInternetConnection_Tick(object sender, EventArgs e)
    {
        #region Check whether the users have internet connection or not
        if (Components.Check_Internet_For_Acceptation() == Status.NoConnection)
        {
            // your logic for no connection
        } else if(Components.Check_Internet_For_Acceptation() == Status.Connected)
        {
            //your logic for connected
        }
        //else status is unknown and nothing will happen till it is connected or no connection
            TimerCheckInternetConnection.Enabled = false;
        }
        #endregion
    }
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171